Class PasswordTokenRequest
Use Credential to access protected resources from the resource server using the
TokenResponse returned by TokenRequest.execute(). On error, it will instead throw
TokenResponseException.
Sample usage:
static void requestAccessToken() throws IOException {
try {
TokenResponse response =
new PasswordTokenRequest(new NetHttpTransport(), new JacksonFactory(),
new GenericUrl("https://server.example.com/token"), "johndoe", "A3ddj3w")
.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.
- Since:
- 1.14
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.api.client.util.GenericData
com.google.api.client.util.GenericData.FlagsNested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringResource owner password.private StringResource owner username.Fields inherited from class TokenRequest
clientAuthentication, requestInitializer -
Constructor Summary
ConstructorsConstructorDescriptionPasswordTokenRequest(com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.http.GenericUrl tokenServerUrl, String username, String password) -
Method Summary
Modifier and TypeMethodDescriptionfinal StringReturns the resource owner password.final StringReturns the resource owner username.setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication) Sets the client authentication ornullfor none.setGrantType(String grantType) Sets the grant type ("authorization_code","password","client_credentials","refresh_token"or absolute URI of the extension grant type).setPassword(String password) Sets the resource owner password.setRequestInitializer(com.google.api.client.http.HttpRequestInitializer requestInitializer) Sets the HTTP request initializer ornullfor none.setScopes(Collection<String> scopes) Sets the list of scopes (as specified in Access Token Scope) ornullfor none.setTokenServerUrl(com.google.api.client.http.GenericUrl tokenServerUrl) Sets the token server URL.setUsername(String username) Sets the resource owner username.Methods inherited from class TokenRequest
execute, executeUnparsed, getClientAuthentication, getGrantType, getJsonFactory, getRequestInitializer, getScopes, getTokenServerUrl, getTransportMethods inherited from class com.google.api.client.util.GenericData
clone, entrySet, get, getClassInfo, getUnknownKeys, put, putAll, remove, setUnknownKeysMethods inherited from class AbstractMap
clear, containsKey, containsValue, equals, hashCode, isEmpty, keySet, size, toString, valuesMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
username
Resource owner username. -
password
Resource owner password.
-
-
Constructor Details
-
PasswordTokenRequest
public PasswordTokenRequest(com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.http.GenericUrl tokenServerUrl, String username, String password) - Parameters:
transport- HTTP transportjsonFactory- JSON factorytokenServerUrl- token server URLusername- resource owner usernamepassword- resource owner password
-
-
Method Details
-
setRequestInitializer
public PasswordTokenRequest setRequestInitializer(com.google.api.client.http.HttpRequestInitializer requestInitializer) Description copied from class:TokenRequestSets the HTTP request initializer ornullfor none.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
setRequestInitializerin classTokenRequest
-
setTokenServerUrl
Description copied from class:TokenRequestSets 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:
setTokenServerUrlin classTokenRequest
-
setScopes
Description copied from class:TokenRequestSets the list of scopes (as specified in Access Token Scope) ornullfor none.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
setScopesin classTokenRequest- Parameters:
scopes- collection of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes)
-
setGrantType
Description copied from class:TokenRequestSets 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:
setGrantTypein classTokenRequest
-
setClientAuthentication
public PasswordTokenRequest setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication) Description copied from class:TokenRequestSets the client authentication ornullfor 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:
setClientAuthenticationin classTokenRequest
-
getUsername
Returns the resource owner username. -
setUsername
Sets the resource owner username.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
-
getPassword
Returns the resource owner password. -
setPassword
Sets the resource owner password.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
-
set
- Overrides:
setin classTokenRequest
-