Class ClientCredentialsTokenRequest
- java.lang.Object
-
- java.util.AbstractMap<java.lang.String,java.lang.Object>
-
- com.google.api.client.util.GenericData
-
- com.google.api.client.auth.oauth2.TokenRequest
-
- com.google.api.client.auth.oauth2.ClientCredentialsTokenRequest
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.util.Map<java.lang.String,java.lang.Object>
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 theTokenResponse
returned byTokenRequest.execute()
. On error, it will instead throwTokenResponseException
.Sample usage:
static void requestAccessToken() throws IOException { try { TokenResponse response = new ClientCredentialsTokenRequest(new NetHttpTransport(), new JacksonFactory(), 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 supportClientParametersAuthentication
. In the above sample code, simply replace the class name and it will work the same way.Implementation is not thread-safe.
- Since:
- 1.14
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.api.client.util.GenericData
com.google.api.client.util.GenericData.Flags
-
-
Field Summary
-
Fields inherited from class com.google.api.client.auth.oauth2.TokenRequest
clientAuthentication, requestInitializer
-
-
Constructor Summary
Constructors Constructor Description ClientCredentialsTokenRequest(com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.http.GenericUrl tokenServerUrl)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ClientCredentialsTokenRequest
set(java.lang.String fieldName, java.lang.Object value)
ClientCredentialsTokenRequest
setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication)
Sets the client authentication ornull
for none.ClientCredentialsTokenRequest
setGrantType(java.lang.String grantType)
Sets the grant type ("authorization_code"
,"password"
,"client_credentials"
,"refresh_token"
or absolute URI of the extension grant type).ClientCredentialsTokenRequest
setRequestInitializer(com.google.api.client.http.HttpRequestInitializer requestInitializer)
Sets the HTTP request initializer ornull
for none.ClientCredentialsTokenRequest
setScopes(java.util.Collection<java.lang.String> scopes)
Sets the list of scopes (as specified in Access Token Scope) ornull
for none.ClientCredentialsTokenRequest
setTokenServerUrl(com.google.api.client.http.GenericUrl tokenServerUrl)
Sets the token server URL.-
Methods inherited from class com.google.api.client.auth.oauth2.TokenRequest
execute, executeUnparsed, getClientAuthentication, getGrantType, getJsonFactory, getRequestInitializer, getScopes, getTokenServerUrl, getTransport
-
Methods inherited from class com.google.api.client.util.GenericData
clone, entrySet, get, getClassInfo, getUnknownKeys, put, putAll, remove, setUnknownKeys
-
Methods inherited from class java.util.AbstractMap
clear, containsKey, containsValue, equals, hashCode, isEmpty, keySet, size, toString, values
-
-
-
-
Constructor Detail
-
ClientCredentialsTokenRequest
public ClientCredentialsTokenRequest(com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.http.GenericUrl tokenServerUrl)
- Parameters:
transport
- HTTP transportjsonFactory
- JSON factorytokenServerUrl
- token server URL
-
-
Method Detail
-
setRequestInitializer
public ClientCredentialsTokenRequest setRequestInitializer(com.google.api.client.http.HttpRequestInitializer requestInitializer)
Description copied from class:TokenRequest
Sets the HTTP request initializer ornull
for none.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
setRequestInitializer
in classTokenRequest
-
setTokenServerUrl
public ClientCredentialsTokenRequest setTokenServerUrl(com.google.api.client.http.GenericUrl tokenServerUrl)
Description copied from class:TokenRequest
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.
- Overrides:
setTokenServerUrl
in classTokenRequest
-
setScopes
public ClientCredentialsTokenRequest setScopes(java.util.Collection<java.lang.String> scopes)
Description copied from class:TokenRequest
Sets the list of scopes (as specified in Access Token Scope) ornull
for none.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
setScopes
in classTokenRequest
- Parameters:
scopes
- collection of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes)
-
setGrantType
public ClientCredentialsTokenRequest setGrantType(java.lang.String grantType)
Description copied from class:TokenRequest
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.
- Overrides:
setGrantType
in classTokenRequest
-
setClientAuthentication
public ClientCredentialsTokenRequest setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication)
Description copied from class:TokenRequest
Sets the client authentication ornull
for none.The recommended initializer by the specification is
BasicAuthentication
. All authorization servers must support that. A common alternative isClientParametersAuthentication
. An alternative client authentication method may be provided that implementsHttpRequestInitializer
.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
TokenRequest.getRequestInitializer()
.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
setClientAuthentication
in classTokenRequest
-
set
public ClientCredentialsTokenRequest set(java.lang.String fieldName, java.lang.Object value)
- Overrides:
set
in classTokenRequest
-
-