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.
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 | |
---|---|
Name | Description |
transport |
com.google.api.client.http.HttpTransport HTTP transport |
jsonFactory |
com.google.api.client.json.JsonFactory JSON factory |
tokenServerUrl |
com.google.api.client.http.GenericUrl token server URL |
grantType |
String 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 | |
---|---|
Name | Description |
transport |
com.google.api.client.http.HttpTransport HTTP transport |
jsonFactory |
com.google.api.client.json.JsonFactory JSON factory |
tokenServerUrl |
com.google.api.client.http.GenericUrl token server URL |
grantType |
String grant type ( |
responseClass |
Class<? 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 | |
---|---|
Type | Description |
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 | |
---|---|
Type | Description |
TokenResponse |
parsed successful access token response |
Exceptions | |
---|---|
Type | Description |
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 | |
---|---|
Type | Description |
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 | |
---|---|
Type | Description |
IOException |
for an error response |
getClientAuthentication()
public final HttpExecuteInterceptor getClientAuthentication()
Returns the client authentication or null
for none.
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Type | Description |
String |
getJsonFactory()
public final JsonFactory getJsonFactory()
Returns the JSON factory.
Returns | |
---|---|
Type | Description |
com.google.api.client.json.JsonFactory |
getRequestInitializer()
public final HttpRequestInitializer getRequestInitializer()
Returns the HTTP request initializer or null
for none.
Returns | |
---|---|
Type | Description |
com.google.api.client.http.HttpRequestInitializer |
getResponseClass()
public final Class<? extends TokenResponse> getResponseClass()
Returns the response class. Must be a subclass of TokenResponse.
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Type | Description |
String |
getTokenServerUrl()
public final GenericUrl getTokenServerUrl()
Returns the token server URL.
Returns | |
---|---|
Type | Description |
com.google.api.client.http.GenericUrl |
getTransport()
public final HttpTransport getTransport()
Returns the HTTP transport.
Returns | |
---|---|
Type | Description |
com.google.api.client.http.HttpTransport |
set(String fieldName, Object value)
public TokenRequest set(String fieldName, Object value)
Parameters | |
---|---|
Name | Description |
fieldName |
String |
value |
Object |
Returns | |
---|---|
Type | Description |
TokenRequest |
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 | |
---|---|
Name | Description |
clientAuthentication |
com.google.api.client.http.HttpExecuteInterceptor |
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Name | Description |
grantType |
String |
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Name | Description |
requestInitializer |
com.google.api.client.http.HttpRequestInitializer |
Returns | |
---|---|
Type | Description |
TokenRequest |
setResponseClass(Class<? extends TokenResponse> responseClass)
public TokenRequest setResponseClass(Class<? extends TokenResponse> responseClass)
Sets the TokenResponse class to allow specifying object parsing.
Parameter | |
---|---|
Name | Description |
responseClass |
Class<? extends com.google.api.client.auth.oauth2.TokenResponse> The response class returned by #execute(). |
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Name | Description |
scopes |
Collection<String> collection of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes) |
Returns | |
---|---|
Type | Description |
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 | |
---|---|
Name | Description |
tokenServerUrl |
com.google.api.client.http.GenericUrl |
Returns | |
---|---|
Type | Description |
TokenRequest |