google.oauth2.credentials_async module

OAuth 2.0 Async Credentials.

This module provides credentials based on OAuth 2.0 access and refresh tokens. These credentials usually access resources on behalf of a user (resource owner).

Specifically, this is intended to use access tokens acquired using the Authorization Code grant and can refresh those tokens using a optional refresh token.

Obtaining the initial access and refresh token is outside of the scope of this module. Consult rfc6749 section 4.1 for complete details on the Authorization Code grant flow.

class Credentials(token, refresh_token=None, id_token=None, token_uri=None, client_id=None, client_secret=None, scopes=None, default_scopes=None, quota_project_id=None, expiry=None, rapt_token=None, refresh_handler=None, enable_reauth_refresh=False, granted_scopes=None, trust_boundary=None, universe_domain='googleapis.com', account=None)[source]

Bases: Credentials

Credentials using OAuth 2.0 access and refresh tokens.

The credentials are considered immutable. If you want to modify the quota project, use with_quota_project() or

credentials = credentials.with_quota_project('myproject-123)
Parameters:
  • token (Optional(str)) – The OAuth 2.0 access token. Can be None if refresh information is provided.

  • refresh_token (str) – The OAuth 2.0 refresh token. If specified, credentials can be refreshed.

  • id_token (str) – The Open ID Connect ID Token.

  • token_uri (str) – The OAuth 2.0 authorization server’s token endpoint URI. Must be specified for refresh, can be left as None if the token can not be refreshed.

  • client_id (str) – The OAuth 2.0 client ID. Must be specified for refresh, can be left as None if the token can not be refreshed.

  • client_secret (str) – The OAuth 2.0 client secret. Must be specified for refresh, can be left as None if the token can not be refreshed.

  • scopes (Sequencestr) – The scopes used to obtain authorization. This parameter is used by has_scopes(). OAuth 2.0 credentials can not request additional scopes after authorization. The scopes must be derivable from the refresh token if refresh information is provided (e.g. The refresh token scopes are a superset of this or contain a wild card scope like ‘https://www.googleapis.com/auth/any-api’).

  • default_scopes (Sequencestr) – Default scopes passed by a Google client library. Use ‘scopes’ for user-defined scopes.

  • quota_project_id (Optionalstr) – The project ID used for quota and billing. This project may be different from the project used to create the credentials.

  • rapt_token (Optionalstr) – The reauth Proof Token.

  • refresh_handler (OptionalCallablegoogle.auth.transport.RequestSequencestr, strdatetime) – A callable which takes in the HTTP request callable and the list of OAuth scopes and when called returns an access token string for the requested scopes and its expiry datetime. This is useful when no refresh tokens are provided and tokens are obtained by calling some external process on demand. It is particularly useful for retrieving downscoped tokens from a token broker.

  • enable_reauth_refresh (Optionalbool) – Whether reauth refresh flow should be used. This flag is for gcloud to use only.

  • granted_scopes (OptionalSequencestr) – The scopes that were consented/granted by the user. This could be different from the requested scopes and it could be empty if granted and requested scopes were same.

  • trust_boundary (str) – String representation of trust boundary meta.

  • universe_domain (Optionalstr) – The universe domain. The default universe domain is googleapis.com.

  • account (Optionalstr) – The account associated with the credential.

async refresh(request)[source]

Refreshes the access token.

Parameters:

request (google.auth.transport.Request) – The object used to make HTTP requests.

Raises:

google.auth.exceptions.RefreshError – If the credentials could not be refreshed.

async before_request(request, method, url, headers)[source]

Performs credential-specific before request logic.

Refreshes the credentials if necessary, then calls apply() to apply the token to the authentication header.

Parameters:
  • request (google.auth.transport.Request) – The object used to make HTTP requests.

  • method (str) – The request’s HTTP method or the RPC method being invoked.

  • url (str) – The request’s URI or the RPC service’s URI.

  • headers (Mapping) – The request’s headers.

property account

The user account associated with the credential. If the account is unknown an empty string is returned.

Type:

str

apply(headers, token=None)[source]

Apply the token to the authentication header.

Parameters:
  • headers (Mapping) – The HTTP request headers.

  • token (Optionalstr) – If specified, overrides the current access token.

property client_id

The OAuth 2.0 client ID.

Type:

Optionalstr

property client_secret

The OAuth 2.0 client secret.

Type:

Optionalstr

property default_scopes

the credentials’ current set of default scopes.

Type:

Sequencestr

property expired

Checks if the credentials are expired.

Note that credentials can be invalid but not expired because Credentials with expiry set to None is considered to never expire.

Deprecated since version v2.24.0: Prefer checking token_state instead.

classmethod from_authorized_user_file(filename, scopes=None)[source]

Creates a Credentials instance from an authorized user json file.

Parameters:
  • filename (str) – The path to the authorized user json file.

  • scopes (Sequencestr) – Optional list of scopes to include in the credentials.

Returns:

The constructed

credentials.

Return type:

google.oauth2.credentials.Credentials

Raises:

ValueError – If the file is not in the expected format.

classmethod from_authorized_user_info(info, scopes=None)[source]

Creates a Credentials instance from parsed authorized user info.

Parameters:
  • info (Mappingstr, str) – The authorized user info in Google format.

  • scopes (Sequencestr) – Optional list of scopes to include in the credentials.

Returns:

The constructed

credentials.

Return type:

google.oauth2.credentials.Credentials

Raises:

ValueError – If the info is not in the expected format.

property granted_scopes

The OAuth 2.0 permission scopes that were granted by the user.

Type:

OptionalSequencestr

has_scopes(scopes)

Checks if the credentials have the given scopes.

Parameters:

scopes (Sequencestr) – The list of scopes to check.

Returns:

True if the credentials have the given scopes.

Return type:

bool

property id_token

The Open ID Connect ID Token.

Depending on the authorization server and the scopes requested, this may be populated when credentials are obtained and updated when refresh() is called. This token is a JWT. It can be verified and decoded using google.oauth2.id_token.verify_oauth2_token().

Type:

Optionalstr

property quota_project_id

Project to use for quota and billing purposes.

property rapt_token

The reauth Proof Token.

Type:

Optionalstr

property refresh_handler

Returns the refresh handler if available.

Returns:

The current refresh handler.

Return type:

OptionalCallablegoogle.auth.transport.RequestSequencestr, strdatetime

property refresh_token

The OAuth 2.0 refresh token.

Type:

Optionalstr

property requires_scopes

OAuth 2.0 credentials have their scopes set when the initial token is requested and can not be changed.

Type:

False

property scopes

The OAuth 2.0 permission scopes.

Type:

Optionalstr

to_json(strip=None)[source]

Utility function that creates a JSON representation of a Credentials object.

Parameters:

strip (Sequencestr) – Optional list of members to exclude from the generated JSON.

Returns:

A JSON representation of this instance. When converted into a dictionary, it can be passed to from_authorized_user_info() to create a new credential instance.

Return type:

str

property token_state

See :obj:`TokenState

property token_uri

The OAuth 2.0 authorization server’s token endpoint URI.

Type:

Optionalstr

property universe_domain

The universe domain value.

property valid

Checks the validity of the credentials.

This is True if the credentials have a token and the token is not expired.

Deprecated since version v2.24.0: Prefer checking token_state instead.

with_account(account)[source]

Returns a copy of these credentials with a modified account.

Parameters:

account (str) – The account to set

Returns:

A new credentials instance.

Return type:

google.oauth2.credentials.Credentials

with_quota_project(quota_project_id)[source]

Returns a copy of these credentials with a modified quota project.

Parameters:

quota_project_id (str) – The project to use for quota and billing purposes

Returns:

A new credentials instance.

Return type:

google.auth.credentials.Credentials

with_token_uri(token_uri)[source]

Returns a copy of these credentials with a modified token uri.

Parameters:

token_uri (str) – The uri to use for fetching/exchanging tokens

Returns:

A new credentials instance.

Return type:

google.auth.credentials.Credentials

with_universe_domain(universe_domain)[source]

Returns a copy of these credentials with a modified universe domain.

Parameters:

universe_domain (str) – The universe domain to use

Returns:

A new credentials instance.

Return type:

google.auth.credentials.Credentials

token

The bearer token that can be used in HTTP headers to make authenticated requests.

Type:

str

expiry

When the token expires and is no longer valid. If this is None, the token is assumed to never expire.

Type:

Optionaldatetime

class UserAccessTokenCredentials(account=None, quota_project_id=None)[source]

Bases: UserAccessTokenCredentials

Access token credentials for user account.

Obtain the access token for a given user account or the current active user account with the gcloud auth print-access-token command.

Parameters:
  • account (Optionalstr) – Account to get the access token for. If not specified, the current active account will be used.

  • quota_project_id (Optionalstr) – The project ID used for quota and billing.

apply(headers, token=None)

Apply the token to the authentication header.

Parameters:
  • headers (Mapping) – The HTTP request headers.

  • token (Optionalstr) – If specified, overrides the current access token.

before_request(request, method, url, headers)[source]

Performs credential-specific before request logic.

Refreshes the credentials if necessary, then calls apply() to apply the token to the authentication header.

Parameters:
  • request (google.auth.transport.Request) – The object used to make HTTP requests.

  • method (str) – The request’s HTTP method or the RPC method being invoked.

  • url (str) – The request’s URI or the RPC service’s URI.

  • headers (Mapping) – The request’s headers.

property expired

Checks if the credentials are expired.

Note that credentials can be invalid but not expired because Credentials with expiry set to None is considered to never expire.

Deprecated since version v2.24.0: Prefer checking token_state instead.

property quota_project_id

Project to use for quota and billing purposes.

refresh(request)[source]

Refreshes the access token.

Parameters:

request (google.auth.transport.Request) – This argument is required by the base class interface but not used in this implementation, so just set it to None.

Raises:

google.auth.exceptions.UserAccessTokenError – If the access token refresh failed.

property token_state

See :obj:`TokenState

property universe_domain

The universe domain value.

property valid

Checks the validity of the credentials.

This is True if the credentials have a token and the token is not expired.

Deprecated since version v2.24.0: Prefer checking token_state instead.

with_account(account)[source]

Create a new instance with the given account.

Parameters:

account (str) – Account to get the access token for.

Returns:

The created

credentials with the given account.

Return type:

google.oauth2.credentials.UserAccessTokenCredentials

with_quota_project(quota_project_id)[source]

Returns a copy of these credentials with a modified quota project.

Parameters:

quota_project_id (str) – The project to use for quota and billing purposes

Returns:

A new credentials instance.

Return type:

google.auth.credentials.Credentials

token

The bearer token that can be used in HTTP headers to make authenticated requests.

Type:

str

expiry

When the token expires and is no longer valid. If this is None, the token is assumed to never expire.

Type:

Optionaldatetime