Google Local Tokens

0.7.6 · active · verified Thu Apr 16

glocaltokens is a Python library designed to extract Google device local authentication tokens, primarily for use in headless systems. It handles the authentication flow using a Google username and password to obtain master tokens, which can then be used to retrieve device-specific tokens. The current version is 0.7.6, and the project maintains an active release cadence, frequently updating dependencies and introducing minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate GlocalTokens, obtain a master token using environment variables for credentials, and print it. It includes basic error handling for authentication failures and advises on 2FA. Obtaining device-specific tokens requires additional setup and discovery (commented out).

import os
from glocaltokens import GlocalTokens, BadAuthenticationError

USERNAME = os.environ.get('GOOGLE_USERNAME', '')
PASSWORD = os.environ.get('GOOGLE_PASSWORD', '')

if not USERNAME or not PASSWORD:
    print("Please set GOOGLE_USERNAME and GOOGLE_PASSWORD environment variables.")
else:
    try:
        gt = GlocalTokens(USERNAME, PASSWORD)
        master_token = gt.get_master_token()
        print(f"Master Token: {master_token}")

        # Example: Get device tokens (requires a device to be connected/discovered)
        # device_tokens = gt.get_device_tokens()
        # print(f"Device Tokens: {device_tokens}")

    except BadAuthenticationError as e:
        print(f"Authentication failed: {e}")
        print("Ensure 2-Factor Authentication is enabled for your Google account.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

view raw JSON →