public class OAuthClient
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ACCEPT_ENCODING
Deprecated.
use
OAuthConsumer.ACCEPT_ENCODING instead |
protected static java.lang.String |
CONTENT_LENGTH |
protected static java.lang.String |
DELETE |
private HttpClient |
http |
protected java.util.Map<java.lang.String,java.lang.Object> |
httpParameters |
static java.lang.String |
PARAMETER_STYLE
The name of the OAuthConsumer property whose value is the ParameterStyle
to be used by invoke.
|
protected static java.lang.String |
POST |
protected static java.lang.String |
PUT |
Constructor and Description |
---|
OAuthClient(HttpClient http) |
Modifier and Type | Method and Description |
---|---|
OAuthResponseMessage |
access(OAuthMessage request,
ParameterStyle style)
Send a request and return the response.
|
OAuthMessage |
getAccessToken(OAuthAccessor accessor,
java.lang.String httpMethod,
java.util.Collection<? extends java.util.Map.Entry> parameters)
Get an access token from the service provider, in exchange for an
authorized request token.
|
HttpClient |
getHttpClient() |
java.util.Map<java.lang.String,java.lang.Object> |
getHttpParameters()
HTTP client parameters, as a map from parameter name to value.
|
void |
getRequestToken(OAuthAccessor accessor)
Get a fresh request token from the service provider.
|
void |
getRequestToken(OAuthAccessor accessor,
java.lang.String httpMethod)
Get a fresh request token from the service provider.
|
void |
getRequestToken(OAuthAccessor accessor,
java.lang.String httpMethod,
java.util.Collection<? extends java.util.Map.Entry> parameters)
Get a fresh request token from the service provider.
|
OAuthMessage |
getRequestTokenResponse(OAuthAccessor accessor,
java.lang.String httpMethod,
java.util.Collection<? extends java.util.Map.Entry> parameters)
Get a fresh request token from the service provider.
|
OAuthMessage |
invoke(OAuthAccessor accessor,
java.lang.String url,
java.util.Collection<? extends java.util.Map.Entry> parameters)
Construct a request message, send it to the service provider and get the
response.
|
OAuthMessage |
invoke(OAuthAccessor accessor,
java.lang.String httpMethod,
java.lang.String url,
java.util.Collection<? extends java.util.Map.Entry> parameters)
Construct a request message, send it to the service provider and get the
response.
|
OAuthMessage |
invoke(OAuthMessage request,
ParameterStyle style)
Send a request message to the service provider and get the response.
|
void |
setHttpClient(HttpClient http) |
private HttpClient http
protected final java.util.Map<java.lang.String,java.lang.Object> httpParameters
public static final java.lang.String PARAMETER_STYLE
@Deprecated public static final java.lang.String ACCEPT_ENCODING
OAuthConsumer.ACCEPT_ENCODING
insteadprotected static final java.lang.String PUT
protected static final java.lang.String POST
protected static final java.lang.String DELETE
protected static final java.lang.String CONTENT_LENGTH
public OAuthClient(HttpClient http)
public void setHttpClient(HttpClient http)
public HttpClient getHttpClient()
public java.util.Map<java.lang.String,java.lang.Object> getHttpParameters()
for parameter names.
public void getRequestToken(OAuthAccessor accessor) throws java.io.IOException, OAuthException, java.net.URISyntaxException
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.OAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
java.net.URISyntaxException
public void getRequestToken(OAuthAccessor accessor, java.lang.String httpMethod) throws java.io.IOException, OAuthException, java.net.URISyntaxException
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.OAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
java.net.URISyntaxException
public void getRequestToken(OAuthAccessor accessor, java.lang.String httpMethod, java.util.Collection<? extends java.util.Map.Entry> parameters) throws java.io.IOException, OAuthException, java.net.URISyntaxException
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.OAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
java.net.URISyntaxException
public OAuthMessage getRequestTokenResponse(OAuthAccessor accessor, java.lang.String httpMethod, java.util.Collection<? extends java.util.Map.Entry> parameters) throws java.io.IOException, OAuthException, java.net.URISyntaxException
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.OAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
java.net.URISyntaxException
public OAuthMessage getAccessToken(OAuthAccessor accessor, java.lang.String httpMethod, java.util.Collection<? extends java.util.Map.Entry> parameters) throws java.io.IOException, OAuthException, java.net.URISyntaxException
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.OAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
java.net.URISyntaxException
public OAuthMessage invoke(OAuthAccessor accessor, java.lang.String httpMethod, java.lang.String url, java.util.Collection<? extends java.util.Map.Entry> parameters) throws java.io.IOException, OAuthException, java.net.URISyntaxException
httpMethod
- the HTTP request method, or null to use the default methodjava.net.URISyntaxException
- the given url isn't valid syntacticallyOAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
public OAuthMessage invoke(OAuthAccessor accessor, java.lang.String url, java.util.Collection<? extends java.util.Map.Entry> parameters) throws java.io.IOException, OAuthException, java.net.URISyntaxException
java.net.URISyntaxException
- the given url isn't valid syntacticallyOAuthProblemException
- the HTTP response status code was not 200 (OK)java.io.IOException
OAuthException
public OAuthMessage invoke(OAuthMessage request, ParameterStyle style) throws java.io.IOException, OAuthException
java.io.IOException
- failed to communicate with the service providerOAuthProblemException
- the HTTP response status code was not 200 (OK)OAuthException
public OAuthResponseMessage access(OAuthMessage request, ParameterStyle style) throws java.io.IOException
java.io.IOException