Google Authentication

0.0.18 · active · verified Thu Apr 16

googleauthentication is a Python meta-package designed to simplify connecting to Google services by bundling and abstracting common patterns from official Google authentication libraries like `google-auth`, `google-auth-oauthlib`, and `google-api-python-client`. It provides helper functions to manage OAuth 2.0 flows for web and installed applications. The current version is 0.0.18, and it maintains an active, iterative release schedule typical for pre-1.0 libraries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `build_oauth_service` to obtain an authenticated Google API client (e.g., Google Drive). It requires a `client_secrets.json` file (downloaded from the Google Cloud Console for your project) and specifies the necessary OAuth scopes. The example will prompt for browser-based authentication if no valid token is found, then list up to 5 files from your Google Drive. Remember to replace placeholder `YOUR_CLIENT_ID` and `YOUR_CLIENT_SECRET` with actual environment variables or ensure your `client_secrets.json` is correctly configured.

import os
import json
from googleauthentication.oauth_session import build_oauth_service

# --- Create a dummy client_secrets.json for this example ---
# In a real scenario, this file is downloaded from Google Cloud Console.
# DO NOT hardcode secrets in production code.
client_secrets_path = "client_secrets.json"
dummy_client_secrets_content = {
    "web": {
        "client_id": os.environ.get("GOOGLE_CLIENT_ID", "YOUR_CLIENT_ID"),
        "project_id": "registry-test-project",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_secret": os.environ.get("GOOGLE_CLIENT_SECRET", "YOUR_CLIENT_SECRET"),
        "redirect_uris": [
            "http://localhost:8080/",
            "http://localhost"
        ]
    }
}
with open(client_secrets_path, "w") as f:
    json.dump(dummy_client_secrets_content, f)
# ---------------------------------------------------------

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
TOKEN_FILE = 'token.json'

try:
    # Build an authenticated Google Drive service
    print("Attempting to build Google Drive service...")
    drive_service = build_oauth_service(
        api_name='drive',
        api_version='v3',
        client_secrets_json=client_secrets_path,
        scopes=SCOPES,
        token_file=TOKEN_FILE
    )
    print("Service built successfully.")

    # Example: List up to 5 files
    print("Fetching files...")
    results = drive_service.files().list(
        pageSize=5, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print("No files found in your Google Drive.")
    else:
        print("Files:")
        for item in items:
            print(f"  {item['name']} ({item['id']})")

except Exception as e:
    print(f"An error occurred during authentication or API call: {e}")
finally:
    # Clean up dummy files created for the example
    if os.path.exists(client_secrets_path):
        os.remove(client_secrets_path)
    if os.path.exists(TOKEN_FILE):
        os.remove(TOKEN_FILE)
    print("Cleanup complete.")

view raw JSON →