Class OAuthClient
- java.lang.Object
-
- net.oauth.client.OAuthClient
-
public class OAuthClient extends Object
Methods for an OAuth consumer to request tokens from a service provider.This class can also be used to request access to protected resources, in some cases. But not in all cases. For example, this class can't handle arbitrary HTTP headers.
Methods of this class return a response as an OAuthMessage, from which you can get a body or parameters but not both. Calling a getParameter method will read and close the body (like readBodyAsString), so you can't read it later. If you read or close the body first, then getParameter can't read it. The response headers should tell you whether the response contains encoded parameters, that is whether you should call getParameter or not.
Methods of this class don't follow redirects. When they receive a redirect response, they throw an OAuthProblemException, with properties HttpResponseMessage.STATUS_CODE = the redirect code HttpResponseMessage.LOCATION = the redirect URL. Such a redirect can't be handled at the HTTP level, if the second request must carry another OAuth signature (with different parameters). For example, Google's Service Provider routinely redirects requests for access to protected resources, and requires the redirected request to be signed.
- Author:
- John Kristian
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOAuthClient.ParameterStyleDeprecated.use net.oauth.ParameterStyle.
-
Field Summary
Fields Modifier and Type Field Description static StringACCEPT_ENCODINGDeprecated.useOAuthConsumer.ACCEPT_ENCODINGinsteadprotected static StringCONTENT_LENGTHprotected static StringDELETEprotected Map<String,Object>httpParametersstatic StringPARAMETER_STYLEThe name of the OAuthConsumer property whose value is the ParameterStyle to be used by invoke.protected static StringPOSTprotected static StringPUT
-
Constructor Summary
Constructors Constructor Description OAuthClient(HttpClient http)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description OAuthResponseMessageaccess(OAuthMessage request, OAuthClient.ParameterStyle style)Deprecated.OAuthResponseMessageaccess(OAuthMessage request, ParameterStyle style)Send a request and return the response.OAuthMessagegetAccessToken(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters)Get an access token from the service provider, in exchange for an authorized request token.HttpClientgetHttpClient()Map<String,Object>getHttpParameters()HTTP client parameters, as a map from parameter name to value.voidgetRequestToken(OAuthAccessor accessor)Get a fresh request token from the service provider.voidgetRequestToken(OAuthAccessor accessor, String httpMethod)Get a fresh request token from the service provider.voidgetRequestToken(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters)Get a fresh request token from the service provider.OAuthMessageinvoke(OAuthAccessor accessor, String httpMethod, String url, Collection<? extends Map.Entry> parameters)Construct a request message, send it to the service provider and get the response.OAuthMessageinvoke(OAuthAccessor accessor, String url, Collection<? extends Map.Entry> parameters)Construct a request message, send it to the service provider and get the response.OAuthMessageinvoke(OAuthMessage request, OAuthClient.ParameterStyle style)Deprecated.OAuthMessageinvoke(OAuthMessage request, ParameterStyle style)Send a request message to the service provider and get the response.voidsetHttpClient(HttpClient http)
-
-
-
Field Detail
-
PARAMETER_STYLE
public static final String PARAMETER_STYLE
The name of the OAuthConsumer property whose value is the ParameterStyle to be used by invoke.- See Also:
- Constant Field Values
-
ACCEPT_ENCODING
@Deprecated public static final String ACCEPT_ENCODING
Deprecated.useOAuthConsumer.ACCEPT_ENCODINGinsteadThe name of the OAuthConsumer property whose value is the Accept-Encoding header in HTTP requests.- See Also:
- Constant Field Values
-
PUT
protected static final String PUT
- See Also:
- Constant Field Values
-
POST
protected static final String POST
- See Also:
- Constant Field Values
-
DELETE
protected static final String DELETE
- See Also:
- Constant Field Values
-
CONTENT_LENGTH
protected static final String CONTENT_LENGTH
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
OAuthClient
public OAuthClient(HttpClient http)
-
-
Method Detail
-
setHttpClient
public void setHttpClient(HttpClient http)
-
getHttpClient
public HttpClient getHttpClient()
-
getHttpParameters
public Map<String,Object> getHttpParameters()
HTTP client parameters, as a map from parameter name to value.- See Also:
for parameter names.
-
getRequestToken
public void getRequestToken(OAuthAccessor accessor) throws IOException, OAuthException, URISyntaxException
Get a fresh request token from the service provider.- Parameters:
accessor- should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.- Throws:
OAuthProblemException- the HTTP response status code was not 200 (OK)IOExceptionOAuthExceptionURISyntaxException
-
getRequestToken
public void getRequestToken(OAuthAccessor accessor, String httpMethod) throws IOException, OAuthException, URISyntaxException
Get a fresh request token from the service provider.- Parameters:
accessor- should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.httpMethod- typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.- Throws:
OAuthProblemException- the HTTP response status code was not 200 (OK)IOExceptionOAuthExceptionURISyntaxException
-
getRequestToken
public void getRequestToken(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, URISyntaxException
Get a fresh request token from the service provider.- Parameters:
accessor- should contain a consumer that contains a non-null consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.requestTokenURL should be the URL (determined by the service provider) for getting a request token.httpMethod- typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.parameters- additional parameters for this request, or null to indicate that there are no additional parameters.- Throws:
OAuthProblemException- the HTTP response status code was not 200 (OK)IOExceptionOAuthExceptionURISyntaxException
-
getAccessToken
public OAuthMessage getAccessToken(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, URISyntaxException
Get an access token from the service provider, in exchange for an authorized request token.- Parameters:
accessor- should contain a non-null requestToken and tokenSecret, and a consumer that contains a consumerKey and consumerSecret. Also, accessor.consumer.serviceProvider.accessTokenURL should be the URL (determined by the service provider) for getting an access token.httpMethod- typically OAuthMessage.POST or OAuthMessage.GET, or null to use the default method.parameters- additional parameters for this request, or null to indicate that there are no additional parameters.- Throws:
OAuthProblemException- the HTTP response status code was not 200 (OK)IOExceptionOAuthExceptionURISyntaxException
-
invoke
public OAuthMessage invoke(OAuthAccessor accessor, String httpMethod, String url, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, URISyntaxException
Construct a request message, send it to the service provider and get the response.- Parameters:
httpMethod- the HTTP request method, or null to use the default method- Returns:
- the response
- Throws:
URISyntaxException- the given url isn't valid syntacticallyOAuthProblemException- the HTTP response status code was not 200 (OK)IOExceptionOAuthException
-
invoke
public OAuthMessage invoke(OAuthAccessor accessor, String url, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, URISyntaxException
Construct a request message, send it to the service provider and get the response.- Returns:
- the response
- Throws:
URISyntaxException- the given url isn't valid syntacticallyOAuthProblemException- the HTTP response status code was not 200 (OK)IOExceptionOAuthException
-
invoke
public OAuthMessage invoke(OAuthMessage request, ParameterStyle style) throws IOException, OAuthException
Send a request message to the service provider and get the response.- Returns:
- the response
- Throws:
IOException- failed to communicate with the service providerOAuthProblemException- the HTTP response status code was not 200 (OK)OAuthException
-
access
public OAuthResponseMessage access(OAuthMessage request, ParameterStyle style) throws IOException
Send a request and return the response. Don't try to decide whether the response indicates success; merely return it.- Throws:
IOException
-
invoke
public OAuthMessage invoke(OAuthMessage request, OAuthClient.ParameterStyle style) throws IOException, OAuthException
Deprecated.- Throws:
IOExceptionOAuthException
-
access
public OAuthResponseMessage access(OAuthMessage request, OAuthClient.ParameterStyle style) throws IOException
Deprecated.- Throws:
IOException
-
-