Google Local Tokens
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
-
ModuleNotFoundError: No module named 'glocaltokens'
cause The `glocaltokens` package is not installed in the current Python environment.fixRun `pip install glocaltokens` in your active Python environment. -
glocaltokens.exceptions.BadAuthenticationError: ...
cause Authentication failed with Google. Common reasons include incorrect username/password, 2-Factor Authentication not enabled, or Google blocking the login attempt.fixVerify your Google username and password are correct. Enable 2-Factor Authentication on your Google account. If issues persist, try logging into Google with the same credentials in a web browser to check for any security prompts. -
AttributeError: module 'protobuf.descriptor' has no attribute 'builder' (or similar protobuf related errors)
cause This typically indicates an incompatibility between the `protobuf` version installed and the version expected by `ghome-foyer-api` (a dependency of `glocaltokens`), or another library in your environment is pinning an incompatible `protobuf` version.fixUse a fresh virtual environment. If the issue persists, try `pip install --upgrade protobuf` to get the latest compatible version, or `pip uninstall protobuf` followed by `pip install glocaltokens` to allow `glocaltokens` to install its preferred version.
Warnings
- breaking The library's internal dependencies, particularly `gpsoauth` and `ghome-foyer-api` (which wraps `protobuf`), have seen significant version bumps. Upgrading `glocaltokens` may require a fresh environment or careful dependency resolution to avoid conflicts with other libraries using older `gpsoauth` or `protobuf` versions.
- gotcha As of v0.6.9, `glocaltokens` requires Python 3.9 or higher. Attempts to install or run the library on older Python versions will fail.
- gotcha Google accounts used for authentication generally require 2-Factor Authentication (2FA) to be enabled for `get_master_token()` to succeed, especially in headless or automated environments. Without 2FA, you might encounter `BadAuthenticationError`.
Install
-
pip install glocaltokens
Imports
- GlocalTokens
from glocaltokens import GlocalTokens
- BadAuthenticationError
from glocaltokens.exceptions import BadAuthenticationError
Quickstart
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}")