public class ClientCredentialsTokenRequest extends TokenRequest
OAuth 2.0 request for an access token using only its client credentials as specified in Client Credentials Grant.
Use Credential 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 { TokenResponse response = new ClientCredentialsTokenRequest(new NetHttpTransport(), new GsonFactory(), new GenericUrl("https://server.example.com/token")) .setRedirectUri("https://client.example.com/rd") .setClientAuthentication( new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw")).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()); } } }
Some OAuth 2.0 providers don't support BasicAuthentication but instead support ClientParametersAuthentication. In the above sample code, simply replace the class name and it will work the same way.
Implementation is not thread-safe.
Inheritance
Object > java.util.AbstractMap > com.google.api.client.util.GenericData > TokenRequest > ClientCredentialsTokenRequestConstructors
ClientCredentialsTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl)
public ClientCredentialsTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl)
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 |
Methods
set(String fieldName, Object value)
public ClientCredentialsTokenRequest set(String fieldName, Object value)
Name | Description |
fieldName | String |
value | Object |
Type | Description |
ClientCredentialsTokenRequest |
setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
public ClientCredentialsTokenRequest 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.
Name | Description |
clientAuthentication | com.google.api.client.http.HttpExecuteInterceptor |
Type | Description |
ClientCredentialsTokenRequest |
setGrantType(String grantType)
public ClientCredentialsTokenRequest 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.
Name | Description |
grantType | String |
Type | Description |
ClientCredentialsTokenRequest |
setRequestInitializer(HttpRequestInitializer requestInitializer)
public ClientCredentialsTokenRequest 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.
Name | Description |
requestInitializer | com.google.api.client.http.HttpRequestInitializer |
Type | Description |
ClientCredentialsTokenRequest |
setResponseClass(Class<? extends TokenResponse> responseClass)
public ClientCredentialsTokenRequest setResponseClass(Class<? extends TokenResponse> responseClass)
Sets the TokenResponse class to allow specifying object parsing.
Name | Description |
responseClass | Class<? extends com.google.api.client.auth.oauth2.TokenResponse> |
Type | Description |
ClientCredentialsTokenRequest |
setScopes(Collection<String> scopes)
public ClientCredentialsTokenRequest 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.
Name | Description |
scopes | Collection<String> |
Type | Description |
ClientCredentialsTokenRequest |
setTokenServerUrl(GenericUrl tokenServerUrl)
public ClientCredentialsTokenRequest 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.
Name | Description |
tokenServerUrl | com.google.api.client.http.GenericUrl |
Type | Description |
ClientCredentialsTokenRequest |