Class GoogleCredentials (1.19.0)

public class GoogleCredentials extends OAuth2Credentials implements QuotaProjectIdProvider

Base type for credentials for authorizing calls to Google APIs using OAuth2.

Inheritance

java.lang.Object > Credentials > OAuth2Credentials > GoogleCredentials

Static Methods

create(AccessToken accessToken)

public static GoogleCredentials create(AccessToken accessToken)

Returns the credentials instance from the given access token.

Parameter
NameDescription
accessTokenAccessToken

the access token

Returns
TypeDescription
GoogleCredentials

the credentials instance

fromStream(InputStream credentialsStream)

public static GoogleCredentials fromStream(InputStream credentialsStream)

Returns credentials defined by a JSON file stream.

The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.

Parameter
NameDescription
credentialsStreamInputStream

the stream with the credential definition.

Returns
TypeDescription
GoogleCredentials

the credential defined by the credentialsStream.

Exceptions
TypeDescription
IOException

if the credential cannot be created from the stream.

fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory)

public static GoogleCredentials fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory)

Returns credentials defined by a JSON file stream.

The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.

Parameters
NameDescription
credentialsStreamInputStream

the stream with the credential definition.

transportFactoryHttpTransportFactory

HTTP transport factory, creates the transport used to get access tokens.

Returns
TypeDescription
GoogleCredentials

the credential defined by the credentialsStream.

Exceptions
TypeDescription
IOException

if the credential cannot be created from the stream.

getApplicationDefault()

public static GoogleCredentials getApplicationDefault()

Returns the Application Default Credentials.

Returns the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:

  1. Credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
  2. Credentials provided by the Google Cloud SDK.
    1. gcloud auth application-default login for user account credentials.
    2. gcloud auth application-default login --impersonate-service-account for impersonated service account credentials.
  • Google App Engine built-in credentials
  • Google Cloud Shell built-in credentials
  • Google Compute Engine built-in credentials
  • Returns
    TypeDescription
    GoogleCredentials

    the credentials instance.

    Exceptions
    TypeDescription
    IOException

    if the credentials cannot be created in the current environment.

    getApplicationDefault(HttpTransportFactory transportFactory)

    public static GoogleCredentials getApplicationDefault(HttpTransportFactory transportFactory)

    Returns the Application Default Credentials.

    Returns the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:

    1. Credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
    2. Credentials provided by the Google Cloud SDK gcloud auth application-default login command
    3. Google App Engine built-in credentials
    4. Google Cloud Shell built-in credentials
    5. Google Compute Engine built-in credentials
    Parameter
    NameDescription
    transportFactoryHttpTransportFactory

    HTTP transport factory, creates the transport used to get access tokens.

    Returns
    TypeDescription
    GoogleCredentials

    the credentials instance.

    Exceptions
    TypeDescription
    IOException

    if the credentials cannot be created in the current environment.

    newBuilder()

    public static GoogleCredentials.Builder newBuilder()
    Returns
    TypeDescription
    GoogleCredentials.Builder

    Constructors

    GoogleCredentials()

    protected GoogleCredentials()

    Default constructor.

    GoogleCredentials(AccessToken accessToken)

    public GoogleCredentials(AccessToken accessToken)

    Constructor with explicit access token.

    Parameter
    NameDescription
    accessTokenAccessToken

    initial or temporary access token

    GoogleCredentials(AccessToken accessToken, String quotaProjectId)

    protected GoogleCredentials(AccessToken accessToken, String quotaProjectId)
    Parameters
    NameDescription
    accessTokenAccessToken
    quotaProjectIdString

    GoogleCredentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)

    protected GoogleCredentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)

    Constructor with explicit access token and refresh times

    Parameters
    NameDescription
    accessTokenAccessToken

    initial or temporary access token

    refreshMarginDuration
    expirationMarginDuration

    GoogleCredentials(GoogleCredentials.Builder builder)

    protected GoogleCredentials(GoogleCredentials.Builder builder)
    Parameter
    NameDescription
    builderGoogleCredentials.Builder

    Fields

    quotaProjectId

    protected final String quotaProjectId
    Field Value
    TypeDescription
    String

    Methods

    createDelegated(String user)

    public GoogleCredentials createDelegated(String user)

    If the credentials support domain-wide delegation, creates a copy of the identity so that it impersonates the specified user; otherwise, returns the same instance.

    Parameter
    NameDescription
    userString

    User to impersonate.

    Returns
    TypeDescription
    GoogleCredentials

    GoogleCredentials with a delegated user.

    createScoped(String[] scopes)

    public GoogleCredentials createScoped(String[] scopes)

    If the credentials support scopes, creates a copy of the identity with the specified scopes; otherwise, returns the same instance.

    Parameter
    NameDescription
    scopesString[]

    Collection of scopes to request.

    Returns
    TypeDescription
    GoogleCredentials

    GoogleCredentials with requested scopes.

    createScoped(Collection<String> scopes)

    public GoogleCredentials createScoped(Collection<String> scopes)

    If the credentials support scopes, creates a copy of the identity with the specified scopes; otherwise, returns the same instance.

    Parameter
    NameDescription
    scopesCollection<String>

    Collection of scopes to request.

    Returns
    TypeDescription
    GoogleCredentials

    GoogleCredentials with requested scopes.

    createScoped(Collection<String> scopes, Collection<String> defaultScopes)

    public GoogleCredentials createScoped(Collection<String> scopes, Collection<String> defaultScopes)

    If the credentials support scopes, creates a copy of the identity with the specified scopes and default scopes; otherwise, returns the same instance. This is mainly used by client libraries.

    Parameters
    NameDescription
    scopesCollection<String>

    Collection of scopes to request.

    defaultScopesCollection<String>

    Collection of default scopes to request.

    Returns
    TypeDescription
    GoogleCredentials

    GoogleCredentials with requested scopes.

    createScopedRequired()

    public boolean createScopedRequired()

    Indicates whether the credentials require scopes to be specified via a call to GoogleCredentials#createScoped before use.

    Returns
    TypeDescription
    boolean

    Whether the credentials require scopes to be specified.

    createWithCustomRetryStrategy(boolean defaultRetriesEnabled)

    public GoogleCredentials createWithCustomRetryStrategy(boolean defaultRetriesEnabled)

    If the credentials support automatic retries, creates a copy of the identity with the provided retry strategy

    Parameter
    NameDescription
    defaultRetriesEnabledboolean

    a flag enabling or disabling default retries

    Returns
    TypeDescription
    GoogleCredentials

    GoogleCredentials with the new default retries configuration.

    createWithQuotaProject(String quotaProject)

    public GoogleCredentials createWithQuotaProject(String quotaProject)

    Creates a credential with the provided quota project.

    Parameter
    NameDescription
    quotaProjectString

    the quota project to set on the credential

    Returns
    TypeDescription
    GoogleCredentials

    credential with the provided quota project

    getAdditionalHeaders()

    protected Map<String,List<String>> getAdditionalHeaders()

    Provide additional headers to return as request metadata.

    Returns
    TypeDescription
    Map<String,List<String>>
    Overrides

    getQuotaProjectId()

    public String getQuotaProjectId()
    Returns
    TypeDescription
    String

    toBuilder()

    public GoogleCredentials.Builder toBuilder()
    Returns
    TypeDescription
    GoogleCredentials.Builder
    Overrides