You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Robert Stagner <re...@gmail.com> on 2011/02/01 07:01:02 UTC

Re: OAuth support for HTTPCient

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:
>
> <dependency>
>     <groupId>net.oauth.core</groupId>
>     <artifactId>oauth-httpclient4</artifactId>
>     <version>20090913</version>
>     <type>jar</type>
>     <scope>compile</scope>
> </dependency>
>
>
> The trick is to add a request interceptor to your HttpClient (code
> included further down):
>
> OAuthCredentials creds = new OAuthCredentials(consumer,secret);
> HttpRequestInterceptor oauthInterceptor = 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 = credentials;
>     }
>
>     public void process(HttpRequest request, HttpContext context)
>               throws HttpException, IOException {
>             AuthState authState = (AuthState)
> context.getAttribute(ClientContext.TARGET_AUTH_STATE);
>             if (authState != null && authState.getAuthScheme() == null) {
>                 AuthScheme scheme = new OAuthSchemeFactory().newInstance(
>                      new BasicHttpParams());
>                 Credentials cred = credentials;
>                  authState.setAuthScheme(scheme);
>                  authState.setCredentials(cred);
>             }
>        }
> }
>
>
> ........
> Jon Moore
> Comcast Interactive Media
>
>
>
>
>
>
> On 1/31/11 12:14 PM, "Robert Stagner" <re...@gmail.com> 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
>
>


-- 
Regards,
Robert

Re: OAuth support for HTTPCient

Posted by "Moore, Jonathan" <Jo...@Comcast.com>.
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" <re...@gmail.com> 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:
>>
>> <dependency>
>>     <groupId>net.oauth.core</groupId>
>>     <artifactId>oauth-httpclient4</artifactId>
>>     <version>20090913</version>
>>     <type>jar</type>
>>     <scope>compile</scope>
>> </dependency>
>>
>>
>> The trick is to add a request interceptor to your HttpClient (code
>> included further down):
>>
>> OAuthCredentials creds = new OAuthCredentials(consumer,secret);
>> HttpRequestInterceptor oauthInterceptor = 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 = credentials;
>>     }
>>
>>     public void process(HttpRequest request, HttpContext context)
>>               throws HttpException, IOException {
>>             AuthState authState = (AuthState)
>> context.getAttribute(ClientContext.TARGET_AUTH_STATE);
>>             if (authState != null && authState.getAuthScheme() == null)
>>{
>>                 AuthScheme scheme = new
>>OAuthSchemeFactory().newInstance(
>>                      new BasicHttpParams());
>>                 Credentials cred = credentials;
>>                  authState.setAuthScheme(scheme);
>>                  authState.setCredentials(cred);
>>             }
>>        }
>> }
>>
>>
>> ........
>> Jon Moore
>> Comcast Interactive Media
>>
>>
>>
>>
>>
>>
>> On 1/31/11 12:14 PM, "Robert Stagner" <re...@gmail.com> 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
>>
>>
>
>
>-- 
>Regards,
>Robert


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org