google_auth_oauthlib package

oauthlib integration for Google Auth

This library provides oauthlib integration with google-auth.

get_user_credentials(scopes, client_id, client_secret)[source]

Gets credentials associated with your Google user account.

This function authenticates using your user credentials by going through the OAuth 2.0 flow. You’ll open a browser window to authenticate to your Google account. The permissions it requests correspond to the scopes you’ve provided.

To obtain the client_id and client_secret, create an OAuth client ID with application type Other from the Credentials page on the Google Developer’s Console. Learn more with the Authenticating as an end user guide.

Parameters:
Returns:

The OAuth 2.0 credentials for the user.

Return type:

google.oauth2.credentials.Credentials

Examples

Get credentials for your user account and use them to run a query with BigQuery:

import google_auth_oauthlib

# TODO: Create a client ID for your project.
client_id = "YOUR-CLIENT-ID.apps.googleusercontent.com"
client_secret = "abc_ThIsIsAsEcReT"

# TODO: Choose the needed scopes for your applications.
scopes = ["https://www.googleapis.com/auth/cloud-platform"]

credentials = google_auth_oauthlib.get_user_credentials(
    scopes, client_id, client_secret
)

# 1. Open the link.
# 2. Authorize the application to have access to your account.
# 3. Copy and paste the authorization code to the prompt.

# Use the credentials to construct a client for Google APIs.
from google.cloud import bigquery

bigquery_client = bigquery.Client(
    credentials=credentials, project="your-project-id"
)
print(list(bigquery_client.query("SELECT 1").result()))