pittgoogle.auth

A class to handle authentication with Google Cloud.

Note

To authenticate, you must have completed one of the setup options described in Authentication. The recommended workflow is to use a service account and set environment variables. In that case, you will not need to call this module directly.

Usage Example

The basic call is:

import pittgoogle

myauth = pittgoogle.auth.Auth()

This will load authentication settings from your environment variables. You can override this behavior with keyword arguments. This does not automatically load the credentials. To do that, request them explicitly:

myauth.credentials

It will first look for a service account key file, then fallback to OAuth2.

class pittgoogle.auth.Auth(GOOGLE_CLOUD_PROJECT=_Nothing.NOTHING, GOOGLE_APPLICATION_CREDENTIALS=_Nothing.NOTHING, OAUTH_CLIENT_ID=_Nothing.NOTHING, OAUTH_CLIENT_SECRET=_Nothing.NOTHING)[source]

Credentials for authentication to a Google Cloud project.

Missing parameters will be obtained from an environment variable of the same name, if it exists.

Parameters
  • GOOGLE_CLOUD_PROJECT (default: NOTHING) – Project ID of the Google Cloud project to connect to.

  • GOOGLE_APPLICATION_CREDENTIALS (default: NOTHING) – Path to a keyfile containing service account credentials. Either this or both OAUTH_CLIENT_* settings are required for successful authentication using Auth.

  • OAUTH_CLIENT_ID (default: NOTHING) – Client ID for an OAuth2 connection. Either this and OAUTH_CLIENT_SECRET, or the GOOGLE_APPLICATION_CREDENTIALS setting, are required for successful authentication using Auth.

  • OAUTH_CLIENT_SECRET (default: NOTHING) – Client secret for an OAuth2 connection. Either this and OAUTH_CLIENT_ID, or the GOOGLE_APPLICATION_CREDENTIALS setting, are required for successful authentication using Auth.

property credentials: Union[Credentials, Credentials]

Credentials, loaded from a service account key file or an OAuth2 session.

_get_credentials()[source]

Load user credentials from a service account key file or an OAuth2 session.

Try the service account first, fall back to OAuth2.

Return type

Union[Credentials, Credentials]

property oauth2: OAuth2Session

requests_oauthlib.OAuth2Session connected to the Google Cloud project.

_authenticate_with_oauth2()[source]

Guide user through authentication and create OAuth2Session for credentials.

The user will need to visit a URL, authenticate themselves, and authorize PittGoogleConsumer to make API calls on their behalf.

The user must have a Google account that is authorized make API calls through the project defined by GOOGLE_CLOUD_PROJECT. In addition, the user must be registered with Pitt-Google (this is a Google requirement on apps that are still in dev).

Return type

OAuth2Session