Class PasswordTokenRequest

  • All Implemented Interfaces:
    java.lang.Cloneable, java.util.Map<java.lang.String,​java.lang.Object>

    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 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.Flags
      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      PasswordTokenRequest​(com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.http.GenericUrl tokenServerUrl, java.lang.String username, java.lang.String password)  
    • Field Detail

      • username

        private java.lang.String username
        Resource owner username.
      • password

        private java.lang.String password
        Resource owner password.
    • Constructor Detail

      • PasswordTokenRequest

        public PasswordTokenRequest​(com.google.api.client.http.HttpTransport transport,
                                    com.google.api.client.json.JsonFactory jsonFactory,
                                    com.google.api.client.http.GenericUrl tokenServerUrl,
                                    java.lang.String username,
                                    java.lang.String password)
        Parameters:
        transport - HTTP transport
        jsonFactory - JSON factory
        tokenServerUrl - token server URL
        username - resource owner username
        password - resource owner password
    • Method Detail

      • setRequestInitializer

        public PasswordTokenRequest setRequestInitializer​(com.google.api.client.http.HttpRequestInitializer requestInitializer)
        Description copied from class: TokenRequest
        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.

        Overrides:
        setRequestInitializer in class TokenRequest
      • setTokenServerUrl

        public PasswordTokenRequest 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 class TokenRequest
      • setScopes

        public PasswordTokenRequest setScopes​(java.util.Collection<java.lang.String> scopes)
        Description copied from class: TokenRequest
        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.

        Overrides:
        setScopes in class TokenRequest
        Parameters:
        scopes - collection of scopes to be joined by a space separator (or a single value containing multiple space-separated scopes)
      • setGrantType

        public PasswordTokenRequest 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 class TokenRequest
      • setClientAuthentication

        public PasswordTokenRequest setClientAuthentication​(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication)
        Description copied from class: TokenRequest
        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 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 class TokenRequest
      • getUsername

        public final java.lang.String getUsername()
        Returns the resource owner username.
      • setUsername

        public PasswordTokenRequest setUsername​(java.lang.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.

      • getPassword

        public final java.lang.String getPassword()
        Returns the resource owner password.
      • setPassword

        public PasswordTokenRequest setPassword​(java.lang.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.