public class

OAuthHelper

extends Object
java.lang.Object
   ↳ com.google.gdata.client.authn.oauth.OAuthHelper
Known Direct Subclasses

Class Overview

Helper methods to support the entire OAuth lifecycle, including generating the user authorization url, exchanging the user authenticated request token for an access token, and generating the Authorization http header.

See Also

Summary

Fields
private String accessTokenUrl
private OAuthHttpClient httpClient
private String requestTokenUrl
private String revokeTokenUrl
private OAuthSigner signer
private String userAuthorizationUrl
Public Constructors
OAuthHelper(String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, OAuthSigner signer)
Create a new OAuthHelper object.
OAuthHelper(String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, OAuthSigner signer, OAuthHttpClient httpClient)
Create a new OAuthHelper object.
OAuthHelper(String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, String revokeTokenUrl, OAuthSigner signer)
Create a new OAuthHelper object.
OAuthHelper(String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, String revokeTokenUrl, OAuthSigner signer, OAuthHttpClient httpClient)
Create a new OAuthHelper object.
Public Methods
String createUserAuthorizationUrl(OAuthParameters oauthParameters)
Generates the url which the user should visit in order to authenticate and authorize with the Service Provider.
String getAccessToken(OAuthParameters oauthParameters)
Exchanges the user-authorized request token for an access token.
String getAccessToken(String queryString, OAuthParameters oauthParameters)
Exchanges the user-authorized request token for an access token.
String getAccessToken(URL url, OAuthParameters oauthParameters)
Exchanges the user-authorized request token for an access token.
String getAccessTokenUrl()
Get the access token url
String getAuthorizationHeader(String requestUrl, String httpMethod, OAuthParameters oauthParameters)
Generates the string to be used as the HTTP authorization header.
void getOAuthParametersFromCallback(URL url, OAuthParameters oauthParameters)
Helper method which parses a url for the OAuth related parameters.
void getOAuthParametersFromCallback(String queryString, OAuthParameters oauthParameters)
Helper method which parses a querystring for the OAuth related parameters.
URL getOAuthUrl(String baseUrl, String httpMethod, OAuthParameters oauthParameters)
Returns a properly formatted and signed OAuth request url, with the appropriate parameters.
String getRequestTokenUrl()
Get the request token url
String getRevokeTokenUrl()
Get the revoke token url
void getUnauthorizedRequestToken(OAuthParameters oauthParameters)
Retrieves the unauthorized request token and token secret from the remote server and sets the parameters in the OAuthParameters object.
String getUserAuthorizationUrl(OAuthParameters oauthParameters)
This method is deprecated. Call a combination of getUnauthorizedRequestToken(OAuthParameters) and createUserAuthorizationUrl(OAuthParameters) instead.
String getUserAuthorizationUrl()
Get the user authorization url
void revokeToken(OAuthParameters oauthParameters)
Revokes the user's OAuth token.
void setAccessTokenUrl(String url)
Set the access token url
void setRequestTokenUrl(String url)
Set the request token url
void setRevokeTokenUrl(String url)
Set the revoke token url
void setUserAuthorizationUrl(String url)
Set the user authorization url
[Expand]
Inherited Methods
From class java.lang.Object

Fields

private String accessTokenUrl

private OAuthHttpClient httpClient

private String requestTokenUrl

private String revokeTokenUrl

private OAuthSigner signer

private String userAuthorizationUrl

Public Constructors

public OAuthHelper (String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, OAuthSigner signer)

Create a new OAuthHelper object.

Parameters
requestTokenUrl The url used to obtain an unauthorized request token
userAuthorizationUrl The url used to obtain user authorization for consumer access
accessTokenUrl The url used to exchange the user-authorized request token for an access token
signer The OAuthSigner to use when signing the request

public OAuthHelper (String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, OAuthSigner signer, OAuthHttpClient httpClient)

Create a new OAuthHelper object. This version of the constructor is primarily for testing purposes, where a mocked OAuthHttpClient and OAuthSigner can be specified.

Parameters
requestTokenUrl The url used to obtain an unauthorized request token
userAuthorizationUrl The url used to obtain user authorization for consumer access
accessTokenUrl The url used to exchange the user-authorized request token for an access token
signer The OAuthSigner to use when signing the request
httpClient The OAuthHttpClient to use when making http requests

public OAuthHelper (String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, String revokeTokenUrl, OAuthSigner signer)

Create a new OAuthHelper object.

Parameters
requestTokenUrl The url used to obtain an unauthorized request token
userAuthorizationUrl The url used to obtain user authorization for consumer access
accessTokenUrl The url used to exchange the user-authorized request token for an access token
revokeTokenUrl The url used to revoke the OAuth token
signer The OAuthSigner to use when signing the request

public OAuthHelper (String requestTokenUrl, String userAuthorizationUrl, String accessTokenUrl, String revokeTokenUrl, OAuthSigner signer, OAuthHttpClient httpClient)

Create a new OAuthHelper object. This version of the constructor is primarily for testing purposes, where a mocked OAuthHttpClient and OAuthSigner can be specified.

Parameters
requestTokenUrl The url used to obtain an unauthorized request token
userAuthorizationUrl The url used to obtain user authorization for consumer access
accessTokenUrl The url used to exchange the user-authorized request token for an access token
revokeTokenUrl The url used to revoke the OAuth token
signer The OAuthSigner to use when signing the request
httpClient The OAuthHttpClient to use when making http requests

Public Methods

public String createUserAuthorizationUrl (OAuthParameters oauthParameters)

Generates the url which the user should visit in order to authenticate and authorize with the Service Provider. This method does not modify the OAuthParameters object. The url will look something like this: https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=[OAUTHTOKENSTRING]&oauth_callback=http%3A%2F%2Fwww.google.com%2F

The following parameter is required in OAuthParameters:

  • oauth_token

The following parameter is optional:

  • oauth_callback

Parameters
oauthParameters The OAuth parameters necessary for this request
Returns
  • The full authorization url the user should visit. The method also modifies the oauthParameters object by adding the request token and token secret.

public String getAccessToken (OAuthParameters oauthParameters)

Exchanges the user-authorized request token for an access token. Typically, this method is called immediately after you extract the user-authorized request token from the authorization response, but it can also be triggered by a user action indicating they've successfully completed authorization with the service provider.

The following parameters are required in OAuthParameters:

  • oauth_consumer_key
  • oauth_token
  • oauth_token_secret (if signing with HMAC)

If the request is successful, the following parameters will be set in OAuthParameters:

  • oauth_token
  • oauth_token_secret (if signing with HMAC)

Parameters
oauthParameters OAuth parameters for this request
Returns
  • The access token. This method also replaces the request token with the access token in the oauthParameters object.
Throws
OAuthException if there is an error with the OAuth request
See Also

public String getAccessToken (String queryString, OAuthParameters oauthParameters)

Exchanges the user-authorized request token for an access token. This method parses the user-authorized request token from the authorization response's query string, and passes it on to getAccessToken(OAuthParameters) (The query string is everything in the authorization response URL after the question mark).

The following parameters are required in OAuthParameters:

  • oauth_consumer_key

Parameters
queryString The query string containing the request token
oauthParameters OAuth parameters for this request
Returns
  • the access token
Throws
OAuthException if there is an error with the OAuth request
See Also

public String getAccessToken (URL url, OAuthParameters oauthParameters)

Exchanges the user-authorized request token for an access token. This method parses the user-authorized request token from the authorization response url, and passes it on to getAccessToken(String, OAuthParameters).

The following parameters are required in OAuthParameters:

  • oauth_consumer_key

Parameters
url The url to parse the request token from
oauthParameters OAuth parameters for this request
Returns
  • the access token
Throws
OAuthException if there is an error with the OAuth request
See Also

public String getAccessTokenUrl ()

Get the access token url

public String getAuthorizationHeader (String requestUrl, String httpMethod, OAuthParameters oauthParameters)

Generates the string to be used as the HTTP authorization header. A typical authorization header will look something like this:

OAuth realm="", oauth_signature="SOME_LONG_STRING", oauth_nonce="123456", oauth_signature_method="RSA-SHA1", oauth_consumer_key="www.example.com", oauth_token="abc123", oauth_timestamp="123456"

The following parameters are required in OAuthParameters:

  • oauth_consumer_key
  • oauth_token
  • oauth_token_secret (if signing with HMAC)

Parameters
requestUrl The url of the request
httpMethod The http method of the request (for example GET)
oauthParameters OAuth parameters for this request
Returns
  • the full authorization header
Throws
OAuthException if there is an error with the OAuth request

public void getOAuthParametersFromCallback (URL url, OAuthParameters oauthParameters)

Helper method which parses a url for the OAuth related parameters. It expects an OAuth token parameter to exist, while the OAuth token secret may or may not exist, depending on the implementation. The parameters are set in the OAuthParameters object.

Parameters
url The url containing the OAuth parameters.
oauthParameters OAuth parameters for this request

public void getOAuthParametersFromCallback (String queryString, OAuthParameters oauthParameters)

Helper method which parses a querystring for the OAuth related parameters. It expects an OAuth token parameter to exist, while the OAuth token secret may or may not exist, depending on the implementation. The parameters are set in the OAuthParameters object.

Parameters
queryString The query string containing the OAuth parameters
oauthParameters OAuth parameters for this request

public URL getOAuthUrl (String baseUrl, String httpMethod, OAuthParameters oauthParameters)

Returns a properly formatted and signed OAuth request url, with the appropriate parameters.

Parameters
baseUrl The url to make the request to
httpMethod The http method of this request (for example, "GET")
oauthParameters OAuth parameters for this request
Returns
  • the OAuth request url
Throws
OAuthException if there is an error with the OAuth request

public String getRequestTokenUrl ()

Get the request token url

public String getRevokeTokenUrl ()

Get the revoke token url

public void getUnauthorizedRequestToken (OAuthParameters oauthParameters)

Retrieves the unauthorized request token and token secret from the remote server and sets the parameters in the OAuthParameters object.

The following parameter is required in OAuthParameters:

  • consumer_key

If the request is successful, the following parameters will be set in OAuthParameters:

  • oauth_token
  • oauth_token_secret (if signing with HMAC)

Parameters
oauthParameters The OAuth parameters necessary for this request.
Throws
OAuthException if there is an error with the OAuth request
See Also

public String getUserAuthorizationUrl (OAuthParameters oauthParameters)

This method is deprecated.
Call a combination of getUnauthorizedRequestToken(OAuthParameters) and createUserAuthorizationUrl(OAuthParameters) instead.

Generates the url which the user should visit in order to authenticate and authorize with the Service Provider. The url will look something like this: https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=[OAUTHTOKENSTRING]&oauth_callback=http%3A%2F%2Fwww.google.com%2F This method first calls getUnauthorizedRequestToken(OAuthParameters) to retrieve the unauthorized request token, and then calls createUserAuthorizationUrl(OAuthParameters). Users who wish to add a token secret to the callback url should call getUnauthorizedRequestToken(OAuthParameters) first, append the retrieved token secret to the callback url, and then call createUserAuthorizationUrl(OAuthParameters).

The following parameter is required in OAuthParameters:

  • oauth_consumer_key

The following parameter is optional:

  • oauth_callback

Parameters
oauthParameters The OAuth parameters necessary for this request
Returns
  • The full authorization url the user should visit. The method also modifies the oauthParameters object by adding the request token and token secret.
Throws
OAuthException if there is an error with the OAuth request
See Also

public String getUserAuthorizationUrl ()

Get the user authorization url

public void revokeToken (OAuthParameters oauthParameters)

Revokes the user's OAuth token. The following parameters are required in OAuthParameters:

  • oauth_consumer_key
  • oauth_token
  • oauth_token_secret (if signing with HMAC)
Only tokens obtained by three-legged OAuth can be revoked (in other words, this method is not applicable to two-legged OAuth).

Parameters
oauthParameters OAuth parameters for this request.
Throws
OAuthException if there is an error with the OAuth request

public void setAccessTokenUrl (String url)

Set the access token url

Parameters
url

public void setRequestTokenUrl (String url)

Set the request token url

Parameters
url

public void setRevokeTokenUrl (String url)

Set the revoke token url

Parameters
url

public void setUserAuthorizationUrl (String url)

Set the user authorization url

Parameters
url