google.auth package

Google Auth Library for Python.

default(scopes=None, request=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).

  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 then the credentials and project ID from the App Identity Service are used.

  4. If the application is running in Compute Engine or the App Engine flexible environment then the credentials and project ID are obtained from the Metadata Service.

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


import google.auth

credentials, project_id = google.auth.default()
  • scopes (Sequence [ str ]) – The list of scopes for the credentials. If specified, the credentials will automatically be scoped if necessary.
  • request (google.auth.transport.Request) – An object used to make HTTP requests. This is used to detect whether the application is running on Compute Engine. If not specified, then it will use the standard library http client to make requests.

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 ] ]


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

load_credentials_from_file(filename, scopes=None)[source]

Loads Google credentials from a file.

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

  • 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.


credentials and the project ID. Authorized user credentials do not have the project ID information.

Return type:

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


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