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" <[email protected]> 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: [email protected]
For additional commands, e-mail: [email protected]