public class PasswordTokenRequest extends TokenRequest
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.
GenericData.Flags
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
responseClass
Constructor and Description |
---|
PasswordTokenRequest(HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
String username,
String password) |
Modifier and Type | Method and Description |
---|---|
String |
getPassword()
Returns the resource owner password.
|
String |
getUsername()
Returns the resource owner username.
|
PasswordTokenRequest |
set(String fieldName,
Object value) |
PasswordTokenRequest |
setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
Sets the client authentication or
null for none. |
PasswordTokenRequest |
setGrantType(String grantType)
Sets the grant type (
"authorization_code" , "password" ,
"client_credentials" , "refresh_token" or absolute URI of the extension grant
type). |
PasswordTokenRequest |
setPassword(String password)
Sets the resource owner password.
|
PasswordTokenRequest |
setRequestInitializer(HttpRequestInitializer requestInitializer)
Sets the HTTP request initializer or
null for none. |
PasswordTokenRequest |
setResponseClass(Class<? extends TokenResponse> responseClass)
Sets the TokenResponse class to allow specifying object parsing.
|
PasswordTokenRequest |
setScopes(Collection<String> scopes)
Sets the list of scopes (as specified in Access Token Scope) or
null
for none. |
PasswordTokenRequest |
setTokenServerUrl(GenericUrl tokenServerUrl)
Sets the token server URL.
|
PasswordTokenRequest |
setUsername(String username)
Sets the resource owner username.
|
execute, executeUnparsed, getClientAuthentication, getGrantType, getJsonFactory, getRequestInitializer, getResponseClass, getScopes, getTokenServerUrl, getTransport
clone, entrySet, equals, get, getClassInfo, getUnknownKeys, hashCode, put, putAll, remove, setUnknownKeys, toString
clear, containsKey, containsValue, isEmpty, keySet, size, values
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public PasswordTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String username, String password)
transport
- HTTP transportjsonFactory
- JSON factorytokenServerUrl
- token server URLusername
- resource owner usernamepassword
- resource owner passwordpublic PasswordTokenRequest setRequestInitializer(HttpRequestInitializer requestInitializer)
TokenRequest
null
for none.
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setRequestInitializer
in class TokenRequest
public PasswordTokenRequest setTokenServerUrl(GenericUrl tokenServerUrl)
TokenRequest
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setTokenServerUrl
in class TokenRequest
public PasswordTokenRequest setScopes(Collection<String> scopes)
TokenRequest
null
for none.
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setScopes
in class TokenRequest
scopes
- collection of scopes to be joined by a space separator (or a single value
containing multiple space-separated scopes)public PasswordTokenRequest setGrantType(String grantType)
TokenRequest
"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.
setGrantType
in class TokenRequest
public PasswordTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication)
TokenRequest
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
TokenRequest.getRequestInitializer()
.
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setClientAuthentication
in class TokenRequest
public PasswordTokenRequest setResponseClass(Class<? extends TokenResponse> responseClass)
TokenRequest
setResponseClass
in class TokenRequest
responseClass
- The response class returned by TokenRequest.execute()
.public final String getUsername()
public PasswordTokenRequest setUsername(String username)
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
public final String getPassword()
public PasswordTokenRequest setPassword(String password)
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
public PasswordTokenRequest set(String fieldName, Object value)
set
in class TokenRequest
Copyright © 2011–2019 Google. All rights reserved.