Skip to main content

Credentials and authorization

Load, refresh, re-authorize, and revoke stored Google OAuth tokens

gclientid stores OAuth client and authorized-user token JSON files under $XDG_CONFIG_HOME/gclientid/ (normally ~/.config/gclientid/).

Loading and refreshing tokens

oauth_creds loads a stored token by account or file path, refreshes it asynchronously when expired, and optionally triggers browser re-authorization when scopes are missing or Google rejects the refresh. refresh_creds refreshes an existing credentials object on a worker thread and saves the updated token.

creds = await oauth_creds(account='me@example.com', scopes=['https://www.googleapis.com/auth/drive'])

oauth_creds

async def oauth_creds(
    token_path=None, scopes=None, account=None, internal:bool=False, desktop:bool=False, reauth=None
):

OAuth creds from a token file, or from the stored token for account (internal and desktop select the client).

reauth runs gclientid-auth in the configured browser when the token is missing, lacks scopes, or can no longer be refreshed; None follows the store’s reauth setting.

@allow is applied at definition so every imported copy is the sandbox-tracked wrapper: an expired token is refreshed on a worker thread, where only a tracked call’s context survives the audit.

refresh_creds

async def refresh_creds(
    creds, token_path=None, reauth=None
):

Refresh creds without blocking the event loop, saving to token_path (default: the file they were loaded from).

When Google rejects the refresh, reauth (None: the store’s setting) runs one gclientid-auth instead of raising.

reauthorize

async def reauthorize(
    token_path, scopes=None
):

Run the stored token’s gclientid-auth in the configured browser, adding scopes, and return the fresh credentials

logout

async def logout(
    token_path=None, account=None, internal:bool=False, desktop:bool=False
):

Revoke and remove the token selected by account (with internal/desktop) or token_path; no-op if it does not exist

token_has_scopes

def token_has_scopes(
    token_path, scopes
):

Whether the authorized-user token file at token_path includes all of scopes

High-level provisioning and account authorization

provision and authorize_account expose the gclientid and gclientid-auth CLI workflows directly as async Python functions.

await provision(desktop=True, cdp_chrome=True)
token_path = await authorize_account('me@example.com', desktop=True, cdp_chrome=True)

provision

async def provision(
    project:str=None, # Google Cloud project ID; the saved one, else the owner's stable `gclientids-*` ID
    name:str='gclientids', # OAuth application and client name
    output:pathlib._local.Path=None, # Credential directory; $XDG_CONFIG_HOME/gclientid if omitted
    owner:str=None, # Existing Cloud account for project creation; otherwise bootstrap through Cloud Console
    account:str=None, # Google account email to authorize when `authorize`
    preset:str=None, # Default scope preset for later authorization; the saved one, else google-apps
    scopes=None, # Additional OAuth scopes to declare, beyond `max`
    apis=None, # Additional Google API service names to enable, beyond `max`
    redirects=None, # Additional Web client redirect URIs, beyond the defaults
    internal:bool=False, # Use the Internal-audience project and its separate *-internal credential files?
    desktop:bool=False, # Ensure the Desktop client instead of the Web client?
    accept_terms:bool=True, # Accept Google's API Services terms automatically?
    cdp_chrome:bool=False, # Use dedicated CDP Chrome instead of normal Chrome?
    authorize:bool=False, # Also authorize `account` after provisioning?
    remote:bool=False, # Authorize through appapis copy/paste instead of the local callback?
    open_browser:bool=True, # Open the appapis authorization URL in the default browser?
):

Ensure the project, its APIs and OAuth app, and one client exist, then optionally authorize an account

authorize_account

async def authorize_account(
    account:str, # Google account email to authorize
    output:pathlib._local.Path=None, # Credential directory; $XDG_CONFIG_HOME/gclientid if omitted
    preset:str=None, # Scope preset; the saved default, else google-apps
    scopes=None, # Additional OAuth scopes
    internal:bool=False, # Use the separately stored Internal OAuth client and token?
    desktop:bool=False, # Use the Desktop client and its token instead of the Web client?
    cdp_chrome:bool=False, # Use dedicated CDP Chrome instead of normal Chrome?
    remote:bool=False, # Use appapis copy/paste instead of the local callback (Web clients only)?
    open_browser:bool=True, # Open the appapis authorization URL in the default browser?
):

Authorize account with an existing OAuth client and save its token

Helpers

token_file

def token_file(
    account:str, # Authorized Google account email
    internal:bool=False, # Token for the Internal-audience client?
    desktop:bool=False, # Token for the Desktop client?
    output:pathlib._local.Path=None, # Credential directory; $XDG_CONFIG_HOME/gclientid if omitted
):

Path of the stored authorized-user token JSON for account

client_file

def client_file(
    internal:bool=False, # The Internal-audience client?
    desktop:bool=False, # The Desktop client instead of the Web client?
    output:pathlib._local.Path=None, # Credential directory; $XDG_CONFIG_HOME/gclientid if omitted
):

Path of the stored OAuth client JSON

reauth_cmd

def reauth_cmd(
    token_path
):

The gclientid-auth command that recreates the token stored at token_path

token_name

def token_name(
    token_path
):

The (account, internal, desktop) a stored token’s filename encodes; account is ’’ for a file named some other way