PyData Google Auth
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
- breaking The default behavior of `get_user_credentials` for `use_local_webserver` changed from `False` to `True` in version 1.4.0. This was due to Google's deprecation of the "out of band" (copy-paste token) OAuth flow. If you were explicitly setting `use_local_webserver=False` and relying on the console-based flow, this might break your application if you update without adjustment.
- breaking Version 1.9.0 dropped support for Python versions older than 3.9. Projects on Python 3.8 or earlier will need to upgrade their Python interpreter to use this version or newer.
- gotcha When developing a tool or library, you should provide your own `client_id` and `client_secret` when calling `get_user_credentials` to prevent masking your API client's identity, as per Google APIs terms of service.
- gotcha For service account credentials, the JSON key file contains sensitive information. It is critical to manage this file securely, never commit it to source control, and use environment variables or secure storage mechanisms in production environments.
- gotcha By default, `pydata-google-auth` caches user credentials on disk in `$HOME/.config/pydata/pydata_google_credentials.json` (or `$APPDATA` on Windows). On shared computing resources like Google Colab or GCE VMs, writing credentials to disk may be undesirable or insecure.
Install
-
pip install pydata-google-auth
Imports
- get_user_credentials
from pydata_google_auth import get_user_credentials
- load_service_account_credentials
from pydata_google_auth import load_service_account_credentials
- default
from pydata_google_auth import default
Quickstart
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}")