Google OAuth (Legacy)

1.0.1 · abandoned · verified Thu Apr 16

This `google-oauth` library (version 1.0.1, last updated in 2016) is an old, unmaintained Python library for handling OAuth2 for Google APIs, primarily focusing on service accounts. It is considered abandoned and should not be used for new development. Modern applications should instead use the actively maintained `google-auth` and `google-auth-oauthlib` libraries for robust and secure Google authentication.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate a service account using the legacy `google-oauth` library. It initializes `ServiceAccount` with a JSON key and obtains an access token. A placeholder `GoogleService` object is shown for making authorized requests. This code should primarily serve as a reference for *legacy* systems; new implementations should use `google-auth` and `google-auth-oauthlib`.

import os
from google_oauth import ServiceAccount, GoogleService

# NOTE: This is for a legacy library. For new projects, use google-auth and google-auth-oauthlib.
# You would typically load the service account key from a file or environment variable.
# For demonstration, we'll use a placeholder. In a real scenario, this would be
# a path to your service account JSON key file.
SERVICE_ACCOUNT_KEY_PATH = os.environ.get('GOOGLE_SERVICE_ACCOUNT_KEY_PATH', 'path/to/your/service_account_key.json')

# The old library's documentation suggests loading from JSON or PKCS12.
# Using a dummy dict to simulate loading from json for demonstration.
# In reality, you'd load a JSON file like this:
# with open(SERVICE_ACCOUNT_KEY_PATH, 'r') as f:
#    key_data = json.load(f)
key_data = {
    "type": "service_account",
    "project_id": "your-project-id",
    "private_key_id": "your-private-key-id",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
    "client_email": "your-service-account-email@developer.gserviceaccount.com",
    "client_id": "your-client-id",
    "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_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account-email%40developer.gserviceaccount.com",
    "universe_domain": "googleapis.com"
}

# Define the required API scopes
SCOPES = ['https://www.googleapis.com/auth/cloud-platform'] # Example scope

try:
    # Initialize ServiceAccount from JSON (or from_pkcs12)
    # The original library's `from_json` expects the dictionary content, not a path.
    # For this example, we'll pass the dummy key_data directly.
    service_account = ServiceAccount.from_json(key_data, SCOPES)

    # The access token is available directly
    access_token = service_account.access_token
    print(f"Successfully obtained access token (legacy): {access_token[:10]}...")

    # Use GoogleService to make an authorized request
    # This part assumes a valid API endpoint exists and would be called.
    # For a real call, replace 'https://www.googleapis.com/compute/v1/projects' with a valid URL
    # and ensure the service account has permissions.
    # This specific example is illustrative, as actual API interaction would require
    # a more complete setup for an actual Google API client.
    google_service = GoogleService(service_account)
    # Example: Attempt to make a dummy request (won't work without a real API call and permissions)
    # resp = google_service.authorized_request('GET', 'https://www.googleapis.com/compute/v1/projects/your-project-id')
    # print(f"Response status: {resp.status_code}")

except Exception as e:
    print(f"An error occurred during legacy google-oauth usage: {e}")
    print("Please note: This library is deprecated. Consider migrating to google-auth and google-auth-oauthlib.")

view raw JSON →