google.oauth2.credentials module

OAuth 2.0 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)[source]

Bases: google.auth.credentials.ReadOnlyScoped, google.auth.credentials.Credentials

Credentials using OAuth 2.0 access and refresh tokens.

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 (Sequence [ str ]) – 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’).

property refresh_token

The OAuth 2.0 refresh token.

Type

Optional [ str ]

property token_uri

The OAuth 2.0 authorization server’s token endpoint URI.

Type

Optional [ str ]

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

Optional [ str ]

property client_id

The OAuth 2.0 client ID.

Type

Optional [ str ]

property client_secret

The OAuth 2.0 client secret.

Type

Optional [ str ]

property requires_scopes

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

Type

False

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.

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

Creates a Credentials instance from parsed authorized user info.

Parameters
  • info (Mapping [ str, str ]) – The authorized user info in Google format.

  • scopes (Sequence [ str ]) – 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.

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 (Sequence [ str ]) – 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.

apply(headers, token=None)[source]

Apply the token to the authentication header.

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

  • token (Optional [ str ]) – 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.

has_scopes(scopes)

Checks if the credentials have the given scopes.

Parameters

scopes (Sequence [ str ]) – The list of scopes to check.

Returns

True if the credentials have the given scopes.

Return type

bool

property scopes

the credentials’ current set of scopes.

Type

Sequence [ str ]

property valid

Checks the validity of the credentials.

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