com.google.api.client.googleapis.auth.oauth2
Class GoogleAuthorizationCodeTokenRequest

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

public class GoogleAuthorizationCodeTokenRequest
extends AuthorizationCodeTokenRequest

Google-specific implementation of the OAuth 2.0 request for an access token based on an authorization code (as specified in Using OAuth 2.0 for Web Server Applications).

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

Sample usage:

  static void requestAccessToken() throws IOException {
    try {
      GoogleTokenResponse response =
          new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
              "812741506391.apps.googleusercontent.com", "{client_secret}",
              "4/P7q7W91a-oMsCeLvIaQm6bTrgtp7", "https://oauth2-login-demo.appspot.com/code")
              .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());
      }
    }
  }
 

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
GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String clientId, String clientSecret, String code, String redirectUri)
           
 
Method Summary
 GoogleTokenResponse execute()
           
 GoogleAuthorizationCodeTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
           
 GoogleAuthorizationCodeTokenRequest setCode(String code)
           
 GoogleAuthorizationCodeTokenRequest setGrantType(String grantType)
           
 GoogleAuthorizationCodeTokenRequest setRedirectUri(String redirectUri)
           
 GoogleAuthorizationCodeTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
           
 GoogleAuthorizationCodeTokenRequest setScopes(Iterable<String> scopes)
           
 GoogleAuthorizationCodeTokenRequest setScopes(String... scopes)
           
 GoogleAuthorizationCodeTokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)
           
 
Methods inherited from class com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest
getCode, getRedirectUri
 
Methods inherited from class com.google.api.client.auth.oauth2.TokenRequest
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

GoogleAuthorizationCodeTokenRequest

public GoogleAuthorizationCodeTokenRequest(HttpTransport transport,
                                           JsonFactory jsonFactory,
                                           String clientId,
                                           String clientSecret,
                                           String code,
                                           String redirectUri)
Parameters:
transport - HTTP transport
jsonFactory - JSON factory
clientId - client identifier issued to the client during the registration process
clientSecret - client secret
code - authorization code generated by the authorization server
redirectUri - redirect URL parameter matching the redirect URL parameter in the authorization request (see setRedirectUri(String)
Method Detail

setRequestInitializer

public GoogleAuthorizationCodeTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
Overrides:
setRequestInitializer in class AuthorizationCodeTokenRequest

setTokenServerUrl

public GoogleAuthorizationCodeTokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)
Overrides:
setTokenServerUrl in class AuthorizationCodeTokenRequest

setScopes

public GoogleAuthorizationCodeTokenRequest setScopes(String... scopes)
Overrides:
setScopes in class AuthorizationCodeTokenRequest

setScopes

public GoogleAuthorizationCodeTokenRequest setScopes(Iterable<String> scopes)
Overrides:
setScopes in class AuthorizationCodeTokenRequest

setGrantType

public GoogleAuthorizationCodeTokenRequest setGrantType(String grantType)
Overrides:
setGrantType in class AuthorizationCodeTokenRequest

setClientAuthentication

public GoogleAuthorizationCodeTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
Overrides:
setClientAuthentication in class AuthorizationCodeTokenRequest

setCode

public GoogleAuthorizationCodeTokenRequest setCode(String code)
Overrides:
setCode in class AuthorizationCodeTokenRequest

setRedirectUri

public GoogleAuthorizationCodeTokenRequest setRedirectUri(String redirectUri)
Overrides:
setRedirectUri in class AuthorizationCodeTokenRequest

execute

public GoogleTokenResponse execute()
                            throws IOException
Overrides:
execute in class TokenRequest
Throws:
IOException


Copyright © 2010-2012 Google. All Rights Reserved.