Skip to content

google_auth

diapason.connectors.google_auth

Shared Google OAuth helpers: access token read + one-shot 401 refresh.

All Google connectors (Gmail, Calendar, Contacts, Drive, Tasks) authenticate with the same OAuth flow and store identical token payloads at ~/.diapason/connectors/*.json — typically a shared google.json file plus per-product copies. They all need the same refresh-on-401 behavior, so the wrapper lives here instead of being duplicated per connector.

Use call_with_refresh(api_fn, credentials_path, *args, **kwargs) around any token-taking API helper. On a 401 the wrapper exchanges the stored refresh_token for a new access_token, updates the credentials file, and retries the call once. All other status codes propagate.

GoogleAuthError

Bases: RuntimeError

Raised when Google credentials are missing or refresh-token grant fails.

call_with_refresh(api_fn, credentials_path, *args, **kwargs)

Invoke api_fn(token, *args, **kwargs) with one-shot 401 auto-refresh.

Loads the current access token from disk, calls the helper, and if Google returns 401 (the access token has expired or been revoked) uses the stored refresh_token to mint a new access_token, updates the credentials file, and retries the call exactly once.

Any other HTTPStatusError is re-raised unchanged — auth-related retries end here; transient 5xx / timeout retries belong further up the stack.

current_access_token(credentials_path)

Return the current access token from the credentials file (empty if absent).

refresh_access_token(credentials_path)

Exchange the stored refresh_token for a fresh access_token and persist it.

Returns the new access_token. Raises :class:GoogleAuthError when the credentials file is missing, lacks a refresh_token / client credentials, or when Google rejects the refresh grant (e.g. the refresh_token has been revoked and the user needs to re-authenticate).

revocation_of(credentials_path)

(quand, pourquoi) si Google a révoqué cet accès, sinon None.