Class PasswordTokenRequest (1.32.1)

public class PasswordTokenRequest extends TokenRequest

OAuth 2.0 request for an access token using the user's username and password as specified in Resource Owner Password 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 PasswordTokenRequest(new NetHttpTransport(), new GsonFactory(), 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.

Inheritance

Object > java.util.AbstractMap > com.google.api.client.util.GenericData > TokenRequest > PasswordTokenRequest

Constructors

PasswordTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String username, String password)

public PasswordTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, String username, String password)
Parameters
NameDescription
transportcom.google.api.client.http.HttpTransport

HTTP transport

jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

tokenServerUrlcom.google.api.client.http.GenericUrl

token server URL

usernameString

resource owner username

passwordString

resource owner password

Methods

getPassword()

public final String getPassword()

Returns the resource owner password.

Returns
TypeDescription
String

getUsername()

public final String getUsername()

Returns the resource owner username.

Returns
TypeDescription
String

set(String fieldName, Object value)

public PasswordTokenRequest set(String fieldName, Object value)
Parameters
NameDescription
fieldNameString
valueObject
Returns
TypeDescription
PasswordTokenRequest
Overrides

setClientAuthentication(HttpExecuteInterceptor clientAuthentication)

public PasswordTokenRequest 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
NameDescription
clientAuthenticationcom.google.api.client.http.HttpExecuteInterceptor
Returns
TypeDescription
PasswordTokenRequest
Overrides

setGrantType(String grantType)

public PasswordTokenRequest 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
NameDescription
grantTypeString
Returns
TypeDescription
PasswordTokenRequest
Overrides

setPassword(String password)

public PasswordTokenRequest setPassword(String password)

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.

Parameter
NameDescription
passwordString
Returns
TypeDescription
PasswordTokenRequest

setRequestInitializer(HttpRequestInitializer requestInitializer)

public PasswordTokenRequest 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
NameDescription
requestInitializercom.google.api.client.http.HttpRequestInitializer
Returns
TypeDescription
PasswordTokenRequest
Overrides

setResponseClass(Class<? extends TokenResponse> responseClass)

public PasswordTokenRequest setResponseClass(Class<? extends TokenResponse> responseClass)

Sets the TokenResponse class to allow specifying object parsing.

Parameter
NameDescription
responseClassClass<? extends com.google.api.client.auth.oauth2.TokenResponse>
Returns
TypeDescription
PasswordTokenRequest
Overrides

setScopes(Collection<String> scopes)

public PasswordTokenRequest 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
NameDescription
scopesCollection<String>
Returns
TypeDescription
PasswordTokenRequest
Overrides

setTokenServerUrl(GenericUrl tokenServerUrl)

public PasswordTokenRequest 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
NameDescription
tokenServerUrlcom.google.api.client.http.GenericUrl
Returns
TypeDescription
PasswordTokenRequest
Overrides

setUsername(String username)

public PasswordTokenRequest setUsername(String username)

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.

Parameter
NameDescription
usernameString
Returns
TypeDescription
PasswordTokenRequest