CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-google-auth-oauthlib

Google Authentication Library - oauthlib integration for OAuth 2.0 flows.

83

1.05x
Overview
Eval results
Files

interactive.mddocs/

Interactive Authentication

Interactive credential acquisition functions designed for development environments like Jupyter notebooks, providing simplified OAuth 2.0 flows with automatic port management and local server handling.

Capabilities

User Credential Acquisition

Main function for obtaining Google OAuth 2.0 credentials through an interactive browser-based flow. Automatically handles local server setup, browser launching, and credential retrieval.

def get_user_credentials(
    scopes: Sequence[str],
    client_id: str,
    client_secret: str,
    minimum_port: int = 8080,
    maximum_port: Optional[int] = None
) -> google.oauth2.credentials.Credentials:
    """
    Gets credentials associated with your Google user account.

    This function authenticates using your user credentials by going through
    the OAuth 2.0 flow. Opens a browser window to authenticate to your
    Google account with permissions corresponding to the provided scopes.

    Args:
        scopes: List of OAuth 2.0 scopes to request during authentication
        client_id: OAuth client ID from Google Developer Console
        client_secret: OAuth client secret from Google Developer Console  
        minimum_port: Beginning of port range for redirect URI HTTP server
        maximum_port: End of port range (exclusive). Tries 100 ports if None

    Returns:
        OAuth 2.0 credentials for the authenticated user

    Raises:
        ConnectionError: If no open port can be found for local server
        ValueError: If client credentials are invalid
    """

Usage Example

import google_auth_oauthlib

# Configure OAuth client credentials (from Google Developer Console)
client_id = "your-app.apps.googleusercontent.com"
client_secret = "your-oauth-client-secret"

# Define required scopes for your application
scopes = [
    'https://www.googleapis.com/auth/cloud-platform',
    'https://www.googleapis.com/auth/userinfo.email'
]

# Obtain credentials interactively
credentials = google_auth_oauthlib.get_user_credentials(
    scopes, client_id, client_secret
)

# Use with Google API clients
from google.cloud import storage
storage_client = storage.Client(credentials=credentials)

Port Management Utilities

Helper functions for managing local server ports during OAuth flows.

def find_open_port(start: int = 8080, stop: Optional[int] = None) -> Optional[int]:
    """
    Find an open port between start and stop.

    Args:
        start: Beginning of range of ports to try (default 8080)
        stop: End of range (exclusive). Tries 100 ports if None

    Returns:
        Available port number, or None if no open port found
    """

def is_port_open(port: int) -> bool:
    """
    Check if a port is open on localhost.

    Args:
        port: Port number to check

    Returns:
        True if port can be bound, False otherwise
    """

Constants

LOCALHOST: str = "localhost"
DEFAULT_PORTS_TO_TRY: int = 100

Common Patterns

Jupyter Notebook Usage

# In Jupyter notebook cell
import google_auth_oauthlib

credentials = google_auth_oauthlib.get_user_credentials(
    scopes=['https://www.googleapis.com/auth/bigquery'],
    client_id="your-client-id.apps.googleusercontent.com",
    client_secret="your-client-secret"
)

# Browser will open automatically for authentication
# After authorization, credentials are ready to use

Custom Port Range

# Use specific port range (e.g., for firewall restrictions)
credentials = google_auth_oauthlib.get_user_credentials(
    scopes=scopes,
    client_id=client_id,
    client_secret=client_secret,
    minimum_port=9000,
    maximum_port=9010
)

Error Handling

try:
    credentials = google_auth_oauthlib.get_user_credentials(
        scopes, client_id, client_secret
    )
except ConnectionError:
    print("Could not find available port for OAuth server")
except ValueError as e:
    print(f"Invalid client configuration: {e}")

Install with Tessl CLI

npx tessl i tessl/pypi-google-auth-oauthlib

docs

cli-tool.md

helpers.md

index.md

interactive.md

oauth-flows.md

tile.json