Return-Path: Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: (qmail 75217 invoked from network); 1 Feb 2011 14:42:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 1 Feb 2011 14:42:37 -0000 Received: (qmail 17006 invoked by uid 500); 1 Feb 2011 14:42:37 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 16733 invoked by uid 500); 1 Feb 2011 14:42:35 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 16717 invoked by uid 99); 1 Feb 2011 14:42:33 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Feb 2011 14:42:33 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [76.96.32.251] (HELO cable.comcast.com) (76.96.32.251) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Feb 2011 14:42:26 +0000 Received: from ([24.40.55.41]) by copdcimo01.cable.comcast.com with ESMTP with TLS id 5503630.24117628; Tue, 01 Feb 2011 07:53:01 -0700 Received: from PACDCEXMB03.cable.comcast.com ([fe80::c9c:28ac:e945:214f]) by PACDCEXHUB02.cable.comcast.com ([fe80::11d4:f530:37a0:9f4e%12]) with mapi id 14.01.0270.001; Tue, 1 Feb 2011 09:42:00 -0500 From: "Moore, Jonathan" To: HttpClient User Discussion Subject: Re: OAuth support for HTTPCient Thread-Topic: OAuth support for HTTPCient Thread-Index: AQHLwWplHmZC9DeKg0+UaRuJNAOzm5PrbBMAgAEPuQCAAD3CgA== Date: Tue, 1 Feb 2011 14:41:58 +0000 Message-ID: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: user-agent: Microsoft-MacOutlook/14.2.0.101115 x-originating-ip: [10.36.191.152] Content-Type: text/plain; charset="us-ascii" Content-ID: <4FF98CB1CC938545934CA282FAAB838A@cable.comcast.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org Hi Robert, Yes, sorry. The "client" is an instance of HttpClient (e.g. DefaultHttpClient). Cheers, Jon ........ Jon Moore Comcast Interactive Media On 2/1/11 1:01 AM, "Robert Stagner" wrote: >Hi Jon, > >That is music to my ears! I assume that the "client" referred to in your >example > >client.addRequestInterceptor(oauthInterceptor, 0); > >points to an instance of HTTPClient. Would that be an accurate statement? > > >On Mon, Jan 31, 2011 at 10:48 AM, Moore, Jonathan < >Jonathan_Moore@comcast.com> wrote: > >> Hi Robert, >> >> As it turns out, you can. You will want to use the "oauth-httpclient4" >> library available here: >> http://code.google.com/p/oauth/ >> >> If you are a Maven user, we use the following dependency: >> >> >> net.oauth.core >> oauth-httpclient4 >> 20090913 >> jar >> compile >> >> >> >> The trick is to add a request interceptor to your HttpClient (code >> included further down): >> >> OAuthCredentials creds =3D new OAuthCredentials(consumer,secret); >> HttpRequestInterceptor oauthInterceptor =3D new OAuthInterceptor(creds); >> client.addRequestInterceptor(oauthInterceptor, 0); >> >> >> Here's the code for OAuthInterceptor.java: >> >> >> import java.io.IOException; >> >> import net.oauth.client.httpclient4.OAuthCredentials; >> import net.oauth.client.httpclient4.OAuthSchemeFactory; >> >> import org.apache.http.HttpException; >> import org.apache.http.HttpRequest; >> import org.apache.http.HttpRequestInterceptor; >> import org.apache.http.auth.AuthScheme; >> import org.apache.http.auth.AuthState; >> import org.apache.http.auth.Credentials; >> import org.apache.http.client.protocol.ClientContext; >> import org.apache.http.params.BasicHttpParams; >> import org.apache.http.protocol.HttpContext; >> >> /** >> * An {@link HttpRequestInterceptor} that adds OAuth credentials to >> requests >> * that are passed to it. An instance of this class is associated with a >> * particular OAuth credential. >> */public class OAuthInterceptor implements HttpRequestInterceptor { >> >> private final OAuthCredentials credentials; >> /** >> * Creates an {@link OAuthInterceptor} using the given credentials. >> * @param credentials The OAuth credentials to add to HTTP requests >> */ >> public OAuthInterceptor(OAuthCredentials credentials) { >> this.credentials =3D credentials; >> } >> >> public void process(HttpRequest request, HttpContext context) >> throws HttpException, IOException { >> AuthState authState =3D (AuthState) >> context.getAttribute(ClientContext.TARGET_AUTH_STATE); >> if (authState !=3D null && authState.getAuthScheme() =3D=3D = null) >>{ >> AuthScheme scheme =3D new >>OAuthSchemeFactory().newInstance( >> new BasicHttpParams()); >> Credentials cred =3D credentials; >> authState.setAuthScheme(scheme); >> authState.setCredentials(cred); >> } >> } >> } >> >> >> ........ >> Jon Moore >> Comcast Interactive Media >> >> >> >> >> >> >> On 1/31/11 12:14 PM, "Robert Stagner" wrote: >> >> >Hi, >> > >> >I'm new to the list, and we've just begun testing a RESTful application >> >that >> >uses OAuth for its authentication. I see from the features supported >>by >> >HTTPClient that OAuth is not on the list >> > >> >Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos >> >authentication schemes. >> > >> >Is there a way to support OAuth within HTTPClient? >> > >> >-- >> >Regards, >> >Robert >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org >> For additional commands, e-mail: httpclient-users-help@hc.apache.org >> >> > > >--=20 >Regards, >Robert --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org