com.google.api.client.auth.oauth2
Class AuthorizationCodeTokenRequest

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.TokenRequest
              extended by com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest
All Implemented Interfaces:
Cloneable, Map<String,Object>

public class AuthorizationCodeTokenRequest
extends TokenRequest

OAuth 2.0 request for an access token using an authorization code as specified in Access Token Request.

Use Credential to access protected resources from the resource server using the TokenResponse returned by TokenRequest.execute(). On error, it will instead throw TokenResponseException.

Sample usage:

  static void requestAccessToken() throws IOException {
    try {
      TokenResponse response =
          new AuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
              new GenericUrl("https://server.example.com/token"), "SplxlOBeZQQYbYS6WxSbIA")
              .setRedirectUri("https://client.example.com/rd")
              .setClientAuthentication(
                  new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw")).execute();
      System.out.println("Access token: " + response.getAccessToken());
    } catch (TokenResponseException e) {
      if (e.getDetails() != null) {
        System.err.println("Error: " + e.getDetails().getError());
        if (e.getDetails().getErrorDescription() != null) {
          System.err.println(e.getDetails().getErrorDescription());
        }
        if (e.getDetails().getErrorUri() != null) {
          System.err.println(e.getDetails().getErrorUri());
        }
      } else {
        System.err.println(e.getMessage());
      }
    }
  }
 

Some OAuth 2.0 providers don't support BasicAuthentication but instead support ClientParametersAuthentication. In the above sample code, simply replace the class name and it will work the same way.

Implementation is not thread-safe.

Since:
1.7
Author:
Yaniv Inbar

Nested Class Summary
 
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>
 
Constructor Summary
AuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String code)
           
 
Method Summary
 String getCode()
          Returns the authorization code generated by the authorization server.
 String getRedirectUri()
          Returns the redirect URI parameter matching the redirect URI parameter in the authorization request or null for none.
 AuthorizationCodeTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
          Sets the client authentication or null for none.
 AuthorizationCodeTokenRequest setCode(String code)
          Sets the authorization code generated by the authorization server.
 AuthorizationCodeTokenRequest setGrantType(String grantType)
          Sets the grant type ("authorization_code", "password", "client_credentials", "refresh_token" or absolute URI of the extension grant type).
 AuthorizationCodeTokenRequest setRedirectUri(String redirectUri)
          Sets the redirect URI parameter matching the redirect URI parameter in the authorization request or null for none.
 AuthorizationCodeTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
          Sets the HTTP request initializer or null for none.
 AuthorizationCodeTokenRequest setScopes(Iterable<String> scopes)
          Sets the list of scopes (as specified in Access Token Scope) or null for none.
 AuthorizationCodeTokenRequest setScopes(String... scopes)
          Sets the list of scopes (as specified in Access Token Scope) or null for none.
 AuthorizationCodeTokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)
          Sets the token server URL.
 
Methods inherited from class com.google.api.client.auth.oauth2.TokenRequest
execute, executeUnparsed, getClientAuthentication, getGrantType, getJsonFactory, getRequestInitializer, getScopes, getTokenServerUrl, getTransport
 
Methods inherited from class com.google.api.client.util.GenericData
clone, entrySet, get, getUnknownKeys, put, putAll, remove, set, setUnknownKeys
 
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
 

Constructor Detail

AuthorizationCodeTokenRequest

public AuthorizationCodeTokenRequest(HttpTransport transport,
                                     JsonFactory jsonFactory,
                                     GenericUrl tokenServerUrl,
                                     String code)
Parameters:
transport - HTTP transport
jsonFactory - JSON factory
tokenServerUrl - token server URL
code - authorization code generated by the authorization server
Method Detail

setRequestInitializer

public AuthorizationCodeTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
Description copied from class: TokenRequest
Sets the HTTP request initializer or null for none.

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

Overrides:
setRequestInitializer in class TokenRequest

setTokenServerUrl

public AuthorizationCodeTokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)
Description copied from class: TokenRequest
Sets the token server URL.

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

Overrides:
setTokenServerUrl in class TokenRequest

setScopes

public AuthorizationCodeTokenRequest setScopes(String... scopes)
Description copied from class: TokenRequest
Sets the list of scopes (as specified in Access Token Scope) or null for none.

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

Overrides:
setScopes in class TokenRequest
Parameters:
scopes - list of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes)

setScopes

public AuthorizationCodeTokenRequest setScopes(Iterable<String> scopes)
Description copied from class: TokenRequest
Sets the list of scopes (as specified in Access Token Scope) or null for none.

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

Overrides:
setScopes in class TokenRequest
Parameters:
scopes - list of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes)

setGrantType

public AuthorizationCodeTokenRequest setGrantType(String grantType)
Description copied from class: TokenRequest
Sets the grant type ("authorization_code", "password", "client_credentials", "refresh_token" or absolute URI of the extension grant type).

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

Overrides:
setGrantType in class TokenRequest

setClientAuthentication

public AuthorizationCodeTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
Description copied from class: TokenRequest
Sets the client authentication or null for none.

The recommended initializer by the specification is BasicAuthentication. All authorization servers must support that. A common alternative is ClientParametersAuthentication. An alternative client authentication method may be provided that implements HttpRequestInitializer.

This HTTP request execute interceptor is guaranteed to be the last execute interceptor before the request is executed, and after any execute interceptor set by the TokenRequest.getRequestInitializer().

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

Overrides:
setClientAuthentication in class TokenRequest

getCode

public final String getCode()
Returns the authorization code generated by the authorization server.


setCode

public AuthorizationCodeTokenRequest setCode(String code)
Sets the authorization code generated by the authorization server.

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.


getRedirectUri

public final String getRedirectUri()
Returns the redirect URI parameter matching the redirect URI parameter in the authorization request or null for none.


setRedirectUri

public AuthorizationCodeTokenRequest setRedirectUri(String redirectUri)
Sets the redirect URI parameter matching the redirect URI parameter in the authorization request or null for none.

Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.



Copyright © 2011-2012 Google. All Rights Reserved.