public class OAuthClient extends 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 String |
ACCEPT_ENCODING
Deprecated.
use
OAuthConsumer.ACCEPT_ENCODING instead |
protected static String |
CONTENT_LENGTH |
protected static String |
DELETE |
protected Map<String,Object> |
httpParameters |
static String |
PARAMETER_STYLE
The name of the OAuthConsumer property whose value is the ParameterStyle
to be used by invoke.
|
protected static String |
POST |
protected static 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,
String httpMethod,
Collection<? extends Map.Entry> parameters)
Get an access token from the service provider, in exchange for an
authorized request token.
|
HttpClient |
getHttpClient() |
Map<String,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,
String httpMethod)
Get a fresh request token from the service provider.
|
void |
getRequestToken(OAuthAccessor accessor,
String httpMethod,
Collection<? extends Map.Entry> parameters)
Get a fresh request token from the service provider.
|
OAuthMessage |
getRequestTokenResponse(OAuthAccessor accessor,
String httpMethod,
Collection<? extends Map.Entry> parameters)
Get a fresh request token from the service provider.
|
OAuthMessage |
invoke(OAuthAccessor accessor,
String url,
Collection<? extends Map.Entry> parameters)
Construct a request message, send it to the service provider and get the
response.
|
OAuthMessage |
invoke(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.
|
OAuthMessage |
invoke(OAuthMessage request,
ParameterStyle style)
Send a request message to the service provider and get the response.
|
void |
setHttpClient(HttpClient http) |
public static final String PARAMETER_STYLE
@Deprecated public static final String ACCEPT_ENCODING
OAuthConsumer.ACCEPT_ENCODING
insteadprotected static final String PUT
protected static final String POST
protected static final String DELETE
protected static final String CONTENT_LENGTH
public OAuthClient(HttpClient http)
public void setHttpClient(HttpClient http)
public HttpClient getHttpClient()
public Map<String,Object> getHttpParameters()
for parameter names.
public void getRequestToken(OAuthAccessor accessor) throws IOException, OAuthException, 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)IOException
OAuthException
URISyntaxException
public void getRequestToken(OAuthAccessor accessor, String httpMethod) throws IOException, OAuthException, 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)IOException
OAuthException
URISyntaxException
public void getRequestToken(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, 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)IOException
OAuthException
URISyntaxException
public OAuthMessage getRequestTokenResponse(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, 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)IOException
OAuthException
URISyntaxException
public OAuthMessage getAccessToken(OAuthAccessor accessor, String httpMethod, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, 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)IOException
OAuthException
URISyntaxException
public OAuthMessage invoke(OAuthAccessor accessor, String httpMethod, String url, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, URISyntaxException
httpMethod
- the HTTP request method, or null to use the default methodURISyntaxException
- the given url isn't valid syntacticallyOAuthProblemException
- the HTTP response status code was not 200 (OK)IOException
OAuthException
public OAuthMessage invoke(OAuthAccessor accessor, String url, Collection<? extends Map.Entry> parameters) throws IOException, OAuthException, URISyntaxException
URISyntaxException
- the given url isn't valid syntacticallyOAuthProblemException
- the HTTP response status code was not 200 (OK)IOException
OAuthException
public OAuthMessage invoke(OAuthMessage request, ParameterStyle style) throws IOException, OAuthException
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 IOException
IOException
Copyright © 2023. All rights reserved.