Class GoogleAuthorizationCodeTokenRequest (2.6.0)

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 GsonFactory(),
         "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.

Inheritance

Object > java.util.AbstractMap > com.google.api.client.util.GenericData > com.google.api.client.auth.oauth2.TokenRequest > com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest > GoogleAuthorizationCodeTokenRequest

Constructors

GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String clientId, String clientSecret, String code, String redirectUri)

public GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String clientId, String clientSecret, String code, String redirectUri)
Parameters
Name Description
transport com.google.api.client.http.HttpTransport

HTTP transport

jsonFactory com.google.api.client.json.JsonFactory

JSON factory

clientId String

client identifier issued to the client during the registration process

clientSecret String

client secret

code String

authorization code generated by the authorization server

redirectUri String

redirect URL parameter matching the redirect URL parameter in the authorization request (see #setRedirectUri(String)

GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String tokenServerEncodedUrl, String clientId, String clientSecret, String code, String redirectUri)

public GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String tokenServerEncodedUrl, String clientId, String clientSecret, String code, String redirectUri)
Parameters
Name Description
transport com.google.api.client.http.HttpTransport

HTTP transport

jsonFactory com.google.api.client.json.JsonFactory

JSON factory

tokenServerEncodedUrl String

token server encoded URL

clientId String

client identifier issued to the client during the registration process

clientSecret String

client secret

code String

authorization code generated by the authorization server

redirectUri String

redirect URL parameter matching the redirect URL parameter in the authorization request (see #setRedirectUri(String)

Methods

execute()

public GoogleTokenResponse execute()
Returns
Type Description
GoogleTokenResponse
Overrides
com.google.api.client.auth.oauth2.TokenRequest.execute()
Exceptions
Type Description
IOException

set(String fieldName, Object value)

public GoogleAuthorizationCodeTokenRequest set(String fieldName, Object value)
Parameters
Name Description
fieldName String
value Object
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.set(java.lang.String,java.lang.Object)

setClientAuthentication(HttpExecuteInterceptor clientAuthentication)

public GoogleAuthorizationCodeTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
Parameter
Name Description
clientAuthentication com.google.api.client.http.HttpExecuteInterceptor
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor)

setCode(String code)

public GoogleAuthorizationCodeTokenRequest setCode(String code)
Parameter
Name Description
code String
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setCode(java.lang.String)

setGrantType(String grantType)

public GoogleAuthorizationCodeTokenRequest setGrantType(String grantType)
Parameter
Name Description
grantType String
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setGrantType(java.lang.String)

setRedirectUri(String redirectUri)

public GoogleAuthorizationCodeTokenRequest setRedirectUri(String redirectUri)
Parameter
Name Description
redirectUri String
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setRedirectUri(java.lang.String)

setRequestInitializer(HttpRequestInitializer requestInitializer)

public GoogleAuthorizationCodeTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
Parameter
Name Description
requestInitializer com.google.api.client.http.HttpRequestInitializer
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

setScopes(Collection<String> scopes)

public GoogleAuthorizationCodeTokenRequest setScopes(Collection<String> scopes)
Parameter
Name Description
scopes Collection<String>
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setScopes(java.util.Collection<java.lang.String>)

setTokenServerUrl(GenericUrl tokenServerUrl)

public GoogleAuthorizationCodeTokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)
Parameter
Name Description
tokenServerUrl com.google.api.client.http.GenericUrl
Returns
Type Description
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setTokenServerUrl(com.google.api.client.http.GenericUrl)