Class UserCredentials

All Implemented Interfaces:
IdTokenProvider, QuotaProjectIdProvider, Serializable

public class UserCredentials extends GoogleCredentials implements IdTokenProvider
OAuth2 Credentials representing a user's identity and consent.
See Also:
  • Field Details

    • GRANT_TYPE

      private static final String GRANT_TYPE
      See Also:
    • PARSE_ERROR_PREFIX

      private static final String PARSE_ERROR_PREFIX
      See Also:
    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • clientId

      private final String clientId
    • clientSecret

      private final String clientSecret
    • refreshToken

      private final String refreshToken
    • tokenServerUri

      private final URI tokenServerUri
    • transportFactoryClassName

      private final String transportFactoryClassName
    • transportFactory

      private transient HttpTransportFactory transportFactory
  • Constructor Details

  • Method Details

    • getMetricsCredentialType

      public CredentialTypeForMetrics getMetricsCredentialType()
      Description copied from class: Credentials
      Gets the credential type used for internal metrics header.

      The default is CredentialTypeForMetrics.DO_NOT_SEND. For a credential that is established to track for metrics, this default should be overridden.

      Overrides:
      getMetricsCredentialType in class Credentials
      Returns:
      a enum value for credential type
    • fromJson

      static UserCredentials fromJson(Map<String,Object> json, HttpTransportFactory transportFactory) throws IOException
      Returns user credentials defined by JSON contents using the format supported by the Cloud SDK.
      Parameters:
      json - a map from the JSON representing the credentials.
      transportFactory - HTTP transport factory, creates the transport used to get access tokens.
      Returns:
      the credentials defined by the JSON.
      Throws:
      IOException - if the credential cannot be created from the JSON.
    • fromStream

      public static UserCredentials fromStream(InputStream credentialsStream) throws IOException
      Returns credentials defined by a JSON file stream using the format supported by the Cloud SDK.
      Parameters:
      credentialsStream - the stream with the credential definition.
      Returns:
      the credential defined by the credentialsStream.
      Throws:
      IOException - if the credential cannot be created from the stream.
    • fromStream

      public static UserCredentials fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException
      Returns credentials defined by a JSON file stream using the format supported by the Cloud SDK.
      Parameters:
      credentialsStream - the stream with the credential definition.
      transportFactory - HTTP transport factory, creates the transport used to get access tokens.
      Returns:
      the credential defined by the credentialsStream.
      Throws:
      IOException - if the credential cannot be created from the stream.
    • refreshAccessToken

      public AccessToken refreshAccessToken() throws IOException
      Refreshes the OAuth2 access token by getting a new access token from the refresh token
      Overrides:
      refreshAccessToken in class OAuth2Credentials
      Returns:
      never
      Throws:
      IOException
    • idTokenWithAudience

      public IdToken idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) throws IOException
      Returns a Google ID Token from the refresh token response.
      Specified by:
      idTokenWithAudience in interface IdTokenProvider
      Parameters:
      targetAudience - This can't be used for UserCredentials.
      options - list of Credential specific options for the token. Currently unused for UserCredentials.
      Returns:
      IdToken object which includes the raw id_token, expiration and audience
      Throws:
      IOException - if the attempt to get an IdToken failed
    • getClientId

      public final String getClientId()
      Returns client ID of the credential from the console.
      Returns:
      client ID
    • getClientSecret

      public final String getClientSecret()
      Returns client secret of the credential from the console.
      Returns:
      client secret
    • getRefreshToken

      public final String getRefreshToken()
      Returns the refresh token resulting from a OAuth2 consent flow.
      Returns:
      refresh token
    • doRefreshAccessToken

      private com.google.api.client.util.GenericData doRefreshAccessToken() throws IOException
      Does refresh access token request
      Returns:
      Refresh token response data
      Throws:
      IOException
    • getUserCredentialsStream

      private InputStream getUserCredentialsStream() throws IOException
      Returns the instance of InputStream containing the following user credentials in JSON format: - RefreshToken - ClientId - ClientSecret - ServerTokenUri
      Returns:
      user credentials stream
      Throws:
      IOException
    • save

      public void save(String filePath) throws IOException
      Saves the end user credentials into the given file path.
      Parameters:
      filePath - Path to file where to store the credentials
      Throws:
      IOException - An error storing the credentials.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class GoogleCredentials
    • toString

      public String toString()
      Overrides:
      toString in class GoogleCredentials
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class GoogleCredentials
    • readObject

      private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException
    • newBuilder

      public static UserCredentials.Builder newBuilder()
    • toBuilder

      public UserCredentials.Builder toBuilder()
      Overrides:
      toBuilder in class GoogleCredentials