Class TokenRequest (1.33.3)

public class TokenRequest extends GenericData

OAuth 2.0 request for an access token as specified in Obtaining Authorization.

Call #execute() to execute the request and use the returned TokenResponse. On error, it will instead throw TokenResponseException.

Implementation is not thread-safe.

Inheritance

Object > java.util.AbstractMap > com.google.api.client.util.GenericData > TokenRequest

Constructors

TokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String grantType)

public TokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String grantType)

Creates a new TokenRequest.

Parameters
NameDescription
transportcom.google.api.client.http.HttpTransport

HTTP transport

jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

tokenServerUrlcom.google.api.client.http.GenericUrl

token server URL

grantTypeString

grant type ("authorization_code", "password", "client_credentials", "refresh_token" or absolute URI of the extension grant type)

TokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String grantType, Class<? extends TokenResponse> responseClass)

public TokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String grantType, Class<? extends TokenResponse> responseClass)

Creates a new TokenRequest.

Parameters
NameDescription
transportcom.google.api.client.http.HttpTransport

HTTP transport

jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

tokenServerUrlcom.google.api.client.http.GenericUrl

token server URL

grantTypeString

grant type ("authorization_code", "password", "client_credentials", "refresh_token" or absolute URI of the extension grant type)

responseClassClass<? extends com.google.api.client.auth.oauth2.TokenResponse>

class used for deserializing the response.

Fields

responseClass

protected Class<? extends TokenResponse> responseClass

Response container class for deserialization.

Field Value
TypeDescription
Class<? extends com.google.api.client.auth.oauth2.TokenResponse>

Methods

execute()

public TokenResponse execute()

Executes request for an access token, and returns the parsed access token response.

To execute but parse the response in an alternate way, use #executeUnparsed().

Default implementation calls #executeUnparsed() and then parses using TokenResponse. Subclasses may override to change the return type, but must still call #executeUnparsed().

Returns
TypeDescription
TokenResponse

parsed successful access token response

Exceptions
TypeDescription
IOException

for an error response

executeUnparsed()

public final HttpResponse executeUnparsed()

Executes request for an access token, and returns the HTTP response.

To execute and parse the response to TokenResponse, instead use #execute().

Callers should call HttpResponse#disconnect when the returned HTTP response object is no longer needed. However, HttpResponse#disconnect does not have to be called if the response stream is properly closed. Example usage:

HttpResponse response = tokenRequest.executeUnparsed(); try { // process the HTTP response object } finally { response.disconnect(); }

Returns
TypeDescription
com.google.api.client.http.HttpResponse

successful access token response, which can then be parsed directly using HttpResponse#parseAs(Class) or some other parsing method

Exceptions
TypeDescription
IOException

for an error response

getClientAuthentication()

public final HttpExecuteInterceptor getClientAuthentication()

Returns the client authentication or null for none.

Returns
TypeDescription
com.google.api.client.http.HttpExecuteInterceptor

getGrantType()

public final String getGrantType()

Returns the grant type ("authorization_code", "password", "client_credentials", "refresh_token" or absolute URI of the extension grant type).

Returns
TypeDescription
String

getJsonFactory()

public final JsonFactory getJsonFactory()

Returns the JSON factory.

Returns
TypeDescription
com.google.api.client.json.JsonFactory

getRequestInitializer()

public final HttpRequestInitializer getRequestInitializer()

Returns the HTTP request initializer or null for none.

Returns
TypeDescription
com.google.api.client.http.HttpRequestInitializer

getResponseClass()

public final Class<? extends TokenResponse> getResponseClass()

Returns the response class. Must be a subclass of TokenResponse.

Returns
TypeDescription
Class<? extends com.google.api.client.auth.oauth2.TokenResponse>

getScopes()

public final String getScopes()

Returns the space-separated list of scopes (as specified in Access Token Scope) or null for none.

Returns
TypeDescription
String

getTokenServerUrl()

public final GenericUrl getTokenServerUrl()

Returns the token server URL.

Returns
TypeDescription
com.google.api.client.http.GenericUrl

getTransport()

public final HttpTransport getTransport()

Returns the HTTP transport.

Returns
TypeDescription
com.google.api.client.http.HttpTransport

set(String fieldName, Object value)

public TokenRequest set(String fieldName, Object value)
Parameters
NameDescription
fieldNameString
valueObject
Returns
TypeDescription
TokenRequest
Overrides
com.google.api.client.util.GenericData.set(java.lang.String,java.lang.Object)

setClientAuthentication(HttpExecuteInterceptor clientAuthentication)

public TokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)

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 #getRequestInitializer().

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

Parameter
NameDescription
clientAuthenticationcom.google.api.client.http.HttpExecuteInterceptor
Returns
TypeDescription
TokenRequest

setGrantType(String grantType)

public TokenRequest setGrantType(String grantType)

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.

Parameter
NameDescription
grantTypeString
Returns
TypeDescription
TokenRequest

setRequestInitializer(HttpRequestInitializer requestInitializer)

public TokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)

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.

Parameter
NameDescription
requestInitializercom.google.api.client.http.HttpRequestInitializer
Returns
TypeDescription
TokenRequest

setResponseClass(Class<? extends TokenResponse> responseClass)

public TokenRequest setResponseClass(Class<? extends TokenResponse> responseClass)

Sets the TokenResponse class to allow specifying object parsing.

Parameter
NameDescription
responseClassClass<? extends com.google.api.client.auth.oauth2.TokenResponse>

The response class returned by #execute().

Returns
TypeDescription
TokenRequest

setScopes(Collection<String> scopes)

public TokenRequest setScopes(Collection<String> scopes)

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.

Parameter
NameDescription
scopesCollection<String>

collection of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes)

Returns
TypeDescription
TokenRequest

setTokenServerUrl(GenericUrl tokenServerUrl)

public TokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)

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.

Parameter
NameDescription
tokenServerUrlcom.google.api.client.http.GenericUrl
Returns
TypeDescription
TokenRequest