google.auth package

Google Auth Library for Python.

default(scopes=None, request=None, quota_project_id=None, default_scopes=None)[source]

Gets the default credentials for the current environment.

Application Default Credentials provides an easy way to obtain credentials to call Google APIs for server-to-server or local applications. This function acquires credentials from the environment in the following order:

  1. If the environment variable GOOGLE_APPLICATION_CREDENTIALS is set to the path of a valid service account JSON private key file, then it is loaded and returned. The project ID returned is the project ID defined in the service account file if available (some older files do not contain project ID information).

    If the environment variable is set to the path of a valid external account JSON configuration file (workload identity federation), then the configuration file is used to determine and retrieve the external credentials from the current environment (AWS, Azure, etc). These will then be exchanged for Google access tokens via the Google STS endpoint. The project ID returned in this case is the one corresponding to the underlying workload identity pool resource if determinable.

    If the environment variable is set to the path of a valid GDCH service account JSON file (Google Distributed Cloud Hosted), then a GDCH credential will be returned. The project ID returned is the project specified in the JSON file.

  2. If the Google Cloud SDK is installed and has application default credentials set they are loaded and returned.

    To enable application default credentials with the Cloud SDK run:

    gcloud auth application-default login
    

    If the Cloud SDK has an active project, the project ID is returned. The active project can be set using:

    gcloud config set project
    
  3. If the application is running in the App Engine standard environment (first generation) then the credentials and project ID from the App Identity Service are used.

  4. If the application is running in Compute Engine or Cloud Run or the App Engine flexible environment or the App Engine standard environment (second generation) then the credentials and project ID are obtained from the Metadata Service.

  5. If no credentials are found, DefaultCredentialsError will be raised.

Example:

import google.auth

credentials, project_id = google.auth.default()
Parameters:
  • scopes (Sequence [ str ]) – The list of scopes for the credentials. If specified, the credentials will automatically be scoped if necessary.

  • request (Optional [ google.auth.transport.Request ]) – An object used to make HTTP requests. This is used to either detect whether the application is running on Compute Engine or to determine the associated project ID for a workload identity pool resource (external account credentials). If not specified, then it will either use the standard library http client to make requests for Compute Engine credentials or a google.auth.transport.requests.Request client for external account credentials.

  • quota_project_id (Optional [ str ]) – The project ID used for quota and billing.

  • default_scopes (Optional [ Sequence [ str ] ]) – Default scopes passed by a Google client library. Use ‘scopes’ for user-defined scopes.

Returns:

the current environment’s credentials and project ID. Project ID may be None, which indicates that the Project ID could not be ascertained from the environment.

Return type:

Tuple [ Credentials, Optional [ str ] ]

Raises:

DefaultCredentialsError – If no credentials were found, or if the credentials found were invalid.

load_credentials_from_file(filename, scopes=None, default_scopes=None, quota_project_id=None, request=None)[source]

Loads Google credentials from a file.

The credentials file must be a service account key, stored authorized user credentials, external account credentials, or impersonated service account credentials.

Parameters:
  • filename (str) – The full path to the credentials file.

  • scopes (Optional [ Sequence [ str ] ]) – The list of scopes for the credentials. If specified, the credentials will automatically be scoped if necessary

  • default_scopes (Optional [ Sequence [ str ] ]) – Default scopes passed by a Google client library. Use ‘scopes’ for user-defined scopes.

  • quota_project_id (Optional [ str ]) – The project ID used for quota and billing.

  • request (Optional [ google.auth.transport.Request ]) – An object used to make HTTP requests. This is used to determine the associated project ID for a workload identity pool resource (external account credentials). If not specified, then it will use a google.auth.transport.requests.Request client to make requests.

Returns:

Loaded

credentials and the project ID. Authorized user credentials do not have the project ID information. External account credentials project IDs may not always be determined.

Return type:

Tuple [ google.auth.credentials.Credentials, Optional [ str ] ]

Raises:

google.auth.exceptions.DefaultCredentialsError – if the file is in the wrong format or is missing.

Subpackages

Submodules