Google Play Services OAuth Client

2.0.0 · active · verified Thu Apr 16

gpsoauth is a Python client library for Google Play Services OAuth, enabling Python applications to use the "master token" flow employed by Android devices for authenticating with Google services. This is particularly useful for projects that need to interact with Google APIs in a manner similar to Android apps. The current version is 2.0.0, and releases occur as needed to address changes in Google's authentication mechanisms or Python compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform a master login to obtain a master token, and then use that token to acquire a service-specific authentication token. It's crucial to use an App Password if Two-Factor Authentication (2FA) is enabled on the Google account. Sensitive credentials are retrieved from environment variables for better security.

import os
import gpsoauth

# It's recommended to retrieve these from environment variables or a secure configuration.
# For a real application, avoid hardcoding sensitive data.
email = os.environ.get('GPSOAUTH_EMAIL', 'your_email@gmail.com')
password = os.environ.get('GPSOAUTH_PASSWORD', 'your_app_password') # Use an app password if 2FA is enabled
android_id = os.environ.get('GPSOAUTH_ANDROID_ID', '0123456789abcdef') # A 16-character hex device ID

try:
    # Perform master login to get the master token
    master_response = gpsoauth.perform_master_login(
        email=email,
        password=password,
        android_id=android_id
    )
    master_token = master_response.get('Token')

    if master_token:
        print(f"Master Token: {master_token[:10]}...")

        # Example: Perform OAuth for Google Play Music service ('sj')
        auth_response = gpsoauth.perform_oauth(
            email=email,
            master_token=master_token,
            android_id=android_id,
            service='sj', # Example service: Google Play Music
            app='com.google.android.music',
            client_sig='38918a453d07199354f8b98840b6156e19f7dc9f' # Example signature for Google Play Music
        )
        auth_token = auth_response.get('Auth')

        if auth_token:
            print(f"Service Auth Token: {auth_token[:10]}...")
        else:
            print(f"Could not get service auth token. Response: {auth_response}")

    else:
        print(f"Could not get master token. Response: {master_response}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure your email, password (or app password), and Android ID are correct.")
    print("If 2-Factor Authentication is enabled, use an App Password instead of your Google account password.")
    print("Also, check if your Google account requires a browser sign-in ('NeedsBrowser' error) or if the service is disabled ('ServiceDisabled').")

view raw JSON →