PyData Google Auth

1.9.1 · active · verified Sun Mar 29

PyData Google Auth is a Python package that provides helper functions for authenticating to Google APIs, simplifying the process of obtaining and caching user and service account credentials. It wraps the underlying `google-auth` and `google-auth-oauthlib` libraries to offer a more convenient interface. The current version is 1.9.1, and the library maintains an active release cadence with frequent minor and patch updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to obtain user credentials using `get_user_credentials` and then use them to initialize a Google Cloud BigQuery client. The process will typically open a browser window for you to authenticate via Google's OAuth 2.0 flow if credentials are not already cached.

import os
from pydata_google_auth import get_user_credentials
from google.cloud import bigquery

# Define the necessary scopes for BigQuery access
SCOPES = [
    'https://www.googleapis.com/auth/cloud-platform',
    'https://www.googleapis.com/auth/bigquery'
]

# Get user credentials (will open a browser for OAuth flow if not cached)
# `use_local_webserver=True` is the default and recommended for desktop apps.
credentials = get_user_credentials(SCOPES)

# Use an environment variable for project_id or replace with your actual ID
project_id = os.environ.get('GCP_PROJECT_ID', 'your-gcp-project-id')

# Initialize a Google Cloud BigQuery client with the obtained credentials
client = bigquery.Client(project=project_id, credentials=credentials)

print(f"Authenticated successfully to project: {client.project}.")
print("You can now use 'client' to interact with BigQuery.")

# Example: List datasets (requires appropriate permissions for the scopes)
try:
    datasets = list(client.list_datasets())
    if datasets:
        print(f"Found {len(datasets)} datasets.")
    else:
        print("No datasets found in the project.")
except Exception as e:
    print(f"Error listing datasets: {e}")

view raw JSON →