com.google.api.client.auth.oauth2.draft10
Class AccessTokenRequest

java.lang.Object
  extended by java.util.AbstractMap<String,Object>
      extended by com.google.api.client.util.GenericData
          extended by com.google.api.client.auth.oauth2.draft10.AccessTokenRequest
All Implemented Interfaces:
Cloneable, Map<String,Object>
Direct Known Subclasses:
AccessTokenRequest.AssertionGrant, AccessTokenRequest.AuthorizationCodeGrant, AccessTokenRequest.RefreshTokenGrant, AccessTokenRequest.ResourceOwnerPasswordCredentialsGrant

public class AccessTokenRequest
extends GenericData

OAuth 2.0 (draft 10) request for an access token as specified in Obtaining an Access Token.

The AccessTokenRequest() or AccessTokenRequest(HttpTransport, JsonFactory, String, String, String) constructors may be used directly when no access grant is included, such as when the client is requesting access to the protected resources under its control. Otherwise, use one of the subclasses, which add custom parameters to specify the access grant. Call execute() to execute the request and use the AccessTokenResponse. On error, use AccessTokenErrorResponse instead.

Sample usage when the client is requesting access to the protected resources under its control:

 
  static void requestAccessToken() throws IOException {
    try {
      AccessTokenRequest request =
          new AccessTokenRequest(new NetHttpTransport(), new JacksonFactory(),
              "https://server.example.com/authorize", "s6BhdRkqt3", "gX1fBat3bV");
      AccessTokenResponse response = request.execute();
      System.out.println("Access token: " + response.accessToken);
    } catch (HttpResponseException e) {
      AccessTokenErrorResponse response = e.response.parseAs(AccessTokenErrorResponse.class);
      System.out.println("Error: " + response.error);
    }
  }
 
 

Since:
1.4
Author:
Yaniv Inbar

Nested Class Summary
static class AccessTokenRequest.AssertionGrant
          OAuth 2.0 Assertion Flow: request an access token based on as assertion as specified in Assertion.
static class AccessTokenRequest.AuthorizationCodeGrant
          OAuth 2.0 Web Server Flow: request an access token based on a verification code as specified in Authorization Code.
static class AccessTokenRequest.GrantType
          Access grant type.
static class AccessTokenRequest.RefreshTokenGrant
          OAuth 2.0 request to refresh an access token as specified in Refresh Token.
static class AccessTokenRequest.ResourceOwnerPasswordCredentialsGrant
          OAuth 2.0 Username and Password Flow: request an access token based on resource owner credentials used in the as specified in Resource Owner Password Credentials.
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
 String authorizationServerUrl
          (REQUIRED) Encoded authorization server URL.
 String clientId
          (REQUIRED, unless the client identity can be establish via other means, for example assertion) The client identifier or null for none.
 String clientSecret
          (REQUIRED) The client secret.
 AccessTokenRequest.GrantType grantType
          (REQUIRED) The access grant type included in the request.
 JsonFactory jsonFactory
          (REQUIRED) JSON factory to use for parsing response in execute().
 String scope
          (OPTIONAL) The scope of the access request expressed as a list of space-delimited strings or null for none.
 HttpTransport transport
          (REQUIRED) HTTP transport required for executing request in execute().
 boolean useBasicAuthorization
          false to specify the password in the request body using the "clientSecret" parameter in the HTTP body or true to use Basic Authentication as recommended in Client Password Credentials.
 
Fields inherited from class com.google.api.client.util.GenericData
unknownFields
 
Constructor Summary
  AccessTokenRequest()
           
protected AccessTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String authorizationServerUrl, String clientSecret)
           
  AccessTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String authorizationServerUrl, String clientId, String clientSecret)
           
 
Method Summary
 AccessTokenResponse execute()
          Executes request for an access token, and returns the parsed access token response.
 HttpResponse executeUnparsed()
          Executes request for an access token, and returns the HTTP response.
 
Methods inherited from class com.google.api.client.util.GenericData
clone, entrySet, get, put, putAll, remove, set
 
Methods inherited from class java.util.AbstractMap
clear, containsKey, containsValue, equals, hashCode, isEmpty, keySet, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

transport

public HttpTransport transport
(REQUIRED) HTTP transport required for executing request in execute().


jsonFactory

public JsonFactory jsonFactory
(REQUIRED) JSON factory to use for parsing response in execute().


grantType

public AccessTokenRequest.GrantType grantType
(REQUIRED) The access grant type included in the request.


clientId

public String clientId
(REQUIRED, unless the client identity can be establish via other means, for example assertion) The client identifier or null for none.


clientSecret

public String clientSecret
(REQUIRED) The client secret.


scope

public String scope
(OPTIONAL) The scope of the access request expressed as a list of space-delimited strings or null for none. The value of the "scope" parameter is defined by the authorization server. If the value contains multiple space-delimited strings, their order does not matter, and each string adds an additional access range to the requested scope. If the access grant being used already represents an approved scope (e.g. authorization code, assertion), the requested scope MUST be equal or lesser than the scope previously granted.


authorizationServerUrl

public String authorizationServerUrl
(REQUIRED) Encoded authorization server URL.


useBasicAuthorization

public boolean useBasicAuthorization
false to specify the password in the request body using the "clientSecret" parameter in the HTTP body or true to use Basic Authentication as recommended in Client Password Credentials.

Defaults to false.

Constructor Detail

AccessTokenRequest

public AccessTokenRequest()

AccessTokenRequest

protected AccessTokenRequest(HttpTransport transport,
                             JsonFactory jsonFactory,
                             String authorizationServerUrl,
                             String clientSecret)
Parameters:
transport - HTTP transport for executing request in execute()
jsonFactory - JSON factory to use for parsing response in execute()
authorizationServerUrl - encoded authorization server URL
clientSecret - client secret

AccessTokenRequest

public AccessTokenRequest(HttpTransport transport,
                          JsonFactory jsonFactory,
                          String authorizationServerUrl,
                          String clientId,
                          String clientSecret)
Parameters:
transport - HTTP transport for executing request in execute()
jsonFactory - JSON factory to use for parsing response in execute()
authorizationServerUrl - encoded authorization server URL
clientId - client identifier
clientSecret - client secret
Method Detail

executeUnparsed

public final HttpResponse executeUnparsed()
                                   throws IOException
Executes request for an access token, and returns the HTTP response.

To execute and parse the response to AccessTokenResponse, use execute()

Returns:
HTTP response, which can then be parsed directly using HttpResponse.parseAs(Class) or some other parsing method
Throws:
HttpResponseException - for an HTTP error response, which can then be parsed using HttpResponse.parseAs(Class) on HttpResponseException.response using AccessTokenErrorResponse
IOException

execute

public final AccessTokenResponse execute()
                                  throws IOException
Executes request for an access token, and returns the parsed access token response.

To execute without parsing the response, use executeUnparsed()

Returns:
parsed access token response
Throws:
HttpResponseException - for an HTTP error response, which can then be parsed using HttpResponse.parseAs(Class) on HttpResponseException.response using AccessTokenErrorResponse
IOException


Copyright © 2010-2011 Google. All Rights Reserved.