Class GoogleAuthorizationCodeTokenRequest (1.35.2)

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
NameDescription
transportcom.google.api.client.http.HttpTransport

HTTP transport

jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

clientIdString

client identifier issued to the client during the registration process

clientSecretString

client secret

codeString

authorization code generated by the authorization server

redirectUriString

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
NameDescription
transportcom.google.api.client.http.HttpTransport

HTTP transport

jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

tokenServerEncodedUrlString

token server encoded URL

clientIdString

client identifier issued to the client during the registration process

clientSecretString

client secret

codeString

authorization code generated by the authorization server

redirectUriString

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

Methods

execute()

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

set(String fieldName, Object value)

public GoogleAuthorizationCodeTokenRequest set(String fieldName, Object value)
Parameters
NameDescription
fieldNameString
valueObject
Returns
TypeDescription
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
NameDescription
clientAuthenticationcom.google.api.client.http.HttpExecuteInterceptor
Returns
TypeDescription
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
NameDescription
codeString
Returns
TypeDescription
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setCode(java.lang.String)

setGrantType(String grantType)

public GoogleAuthorizationCodeTokenRequest setGrantType(String grantType)
Parameter
NameDescription
grantTypeString
Returns
TypeDescription
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setGrantType(java.lang.String)

setRedirectUri(String redirectUri)

public GoogleAuthorizationCodeTokenRequest setRedirectUri(String redirectUri)
Parameter
NameDescription
redirectUriString
Returns
TypeDescription
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setRedirectUri(java.lang.String)

setRequestInitializer(HttpRequestInitializer requestInitializer)

public GoogleAuthorizationCodeTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
Parameter
NameDescription
requestInitializercom.google.api.client.http.HttpRequestInitializer
Returns
TypeDescription
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
NameDescription
scopesCollection<String>
Returns
TypeDescription
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
NameDescription
tokenServerUrlcom.google.api.client.http.GenericUrl
Returns
TypeDescription
GoogleAuthorizationCodeTokenRequest
Overrides
com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest.setTokenServerUrl(com.google.api.client.http.GenericUrl)