How gog Talks to Google
A command such as gog gmail search relies on configuration in a Google Cloud project, local files, and credentials in a keyring. This chapter explains how those pieces fit together so you can predict which permissions gog auth add requests, diagnose token failures, choose a keyring backend, and locate gog’s files.
For step-by-step setup instructions, see Authentication and Accounts.
The whole picture
Register gog as an OAuth application in your own Google Cloud project. It then calls Google APIs on behalf of the accounts you authorize. Figure 1.1 shows when each component is used.
Figure 1.1. Blue steps happen once per OAuth client, orange steps once per Google account, and green steps on every command. The purple box is the state that gog keeps on your machine.
The Google side
Google Cloud project and enabled APIs
A Google Cloud project is the container in which Google records your OAuth application, its consent screen and the APIs it may call. gog uses your own project, so the quota and the consent screen your accounts see belong to you.
Each Google API must be enabled in the project before any client can call it. This book uses 2 APIs, the Gmail API (gmail.googleapis.com) and the Google Calendar API (calendar-json.googleapis.com). These are the identifiers that gog auth setup prints when you select the gmail and calendar services.
OAuth client and its JSON credentials
An OAuth client is the registration of one application with Google’s authorization server. It consists of a client ID, which is public, and a client secret. gog expects a client of the type Desktop app, which lets it receive the authorization result on a loopback address such as http://127.0.0.1:<port>/oauth2/callback.
When you create the client in the Cloud console, Google offers it as a JSON file, usually named client_secret_<something>.json. This file is the only thing gog needs from the Cloud console.
gog auth credentials reads it and splits it: the client ID goes into credentials.json in gog’s data directory, and the client secret goes into the keyring, unless you pass --insecure, which keeps the secret in the JSON file. The OAuth clients guide describes the storage rules.
Consent screen and publishing status
The OAuth consent screen is the page Google shows when an account authorizes your client: it names the app and lists the permissions requested. For personal use, the gog quickstart recommends the user type “External” with your own address as the contact.
The consent screen also has a publishing status, either “Testing” or “In production”. This status matters because of token lifetime. According to the gog quickstart, an External app in Testing receives refresh tokens for user-data scopes that expire after 7 days, which forces you to sign in again every week.
The quickstart’s remedy is to publish the app: on the consent screen’s Audience page, choose “Publish app” and confirm. This moves the app to “In production” without submitting it for Google’s verification.
An unverified app in production still works for your own accounts, but Google shows a warning for sensitive scopes and limits the app to 100 users over its lifetime. If you authorized while the app was in Testing, run gog auth add once more with the same services and --force-consent.
The Cloud console records the publishing status. When gog auth doctor --check encounters invalid_grant, it prompts you to check that the OAuth app is published so its tokens can last longer (auth_doctor.go).
Scopes
A scope is a named permission, written as a URL, that an OAuth client requests for an account. Google shows the requested scopes on the consent screen, and the resulting tokens can do only what those scopes allow.
You rarely type scope URLs with gog. Instead, you name services with --services, and gog maps each service to its scopes. gog auth services prints the full table, including the 2 entries below.
| Service | Scopes requested by default |
|---|---|
gmail | gmail.modify, gmail.settings.basic, gmail.settings.sharing |
calendar | calendar |
All scope names in the table are short forms of URLs under https://www.googleapis.com/auth/. In addition to the service scopes, gog auth add always requests openid, email and userinfo.email, which let gog confirm which account signed in.
You can narrow the request with 3 options.
--readonlyongog auth addasks for read-only scopes. Forgmail,calendar, the read-only scopes aregmail.readonlyandcalendar.readonly. The same flag on any other command blocks mutating API requests at runtime.--gmail-scopechooses the Gmail scopes:full(the default),readonly,send(onlygmail.send) orread-send(gmail.readonlyplusgmail.send).--servicesitself: its default value isuser, which selects every default user service, from Gmail and Calendar to YouTube and Photos. For this book, always pass--services gmail,calendarso that the consent screen asks only for what you use.
You can preview the exact list before any browser opens: gog --dry-run auth add you@example.com --services gmail,calendar prints the plan, including a scopes array. Authentication and Accounts shows that output.
Refresh tokens and access tokens
OAuth gives gog 2 types of tokens.
| Token | What it is | Lifetime | Where gog keeps it |
|---|---|---|---|
| Refresh token | A long-lived credential that proves the account approved your client | Until it is revoked, expires under a policy (such as the 7-day Testing rule), or is replaced | The keyring entry token:<client>:<email> |
| Access token | A short-lived credential sent with every API request | About 1 hour | Cached in the same keyring entry, with its expiration |
When a command needs to call Google, gog loads the keyring entry, reuses the cached access token if it is still valid, and otherwise exchanges the refresh token for a new access token, which it writes back to the entry (client_auth.go). You never handle access tokens yourself in normal use.
If Google rejects the refresh token, the error contains invalid_grant. gog then reports that the token expired or was revoked and tells you to run gog auth add <email> again. The global flag --access-token (or GOG_ACCESS_TOKEN) bypasses stored tokens entirely: gog uses the token you give it, and the help text warns that such a token expires in about 1 hour with no automatic renewal.
The keyring
A keyring is a store for secrets. gog stores OAuth client secrets, refresh tokens, and cached access tokens there. The backend is chosen by GOG_KEYRING_BACKEND if it is set, otherwise by keyring_backend in config.json, otherwise it is auto.
| Backend | Where secrets go |
|---|---|
auto | The store that the keyring library finds on the system: Keychain on macOS, Secret Service on Linux, Credential Manager on Windows |
keychain | Only the keyring library’s keychain backend, which is the macOS Keychain |
file | Encrypted files in the keyring directory under gog’s data directory |
The file backend encrypts every entry with a password. gog reads it from GOG_KEYRING_PASSWORD; without that variable it prompts on the terminal, and in a process with no terminal (a service, CI job or agent) it fails with no TTY available for keyring file backend password prompt; set GOG_KEYRING_PASSWORD.
Headless Linux is a special case. With the auto backend on Linux, gog uses the file backend whenever DBUS_SESSION_BUS_ADDRESS is unset or set to disabled:, because Secret Service needs a D-Bus session and would otherwise hang (backend.go). The paths guide recommends exactly that setting for containers and agents, with the password supplied by a secret manager.
The operating system’s keyring groups gog entries under the service name gogcli (or the value of GOG_KEYRING_SERVICE_NAME) regardless of GOG_HOME. A separate GOG_HOME therefore gives you a separate set of tokens only with the file backend.
Accounts, aliases and clients
An account is a Google account email address for which gog holds a refresh token. When a command needs an account, gog takes the first of these that is available (account.go):
- The
--accountflag (short form-a). - The
GOG_ACCOUNTenvironment variable. - The stored default account, which you set in the browser-based account manager,
gog auth manage. - The only stored token, if exactly one exists.
If none applies, the command stops with missing --account. The value auto in --account or GOG_ACCOUNT skips the first 2 steps and asks for this automatic choice.
An alias is a short name for an account, such as me for you@example.com or work for you@example.org. gog stores aliases in config.json under account_aliases, and both --account and GOG_ACCOUNT accept them. The names auto and default are reserved.
A client name selects one OAuth client among several, with --client or GOG_CLIENT. The default client is called default and uses credentials.json; a client named work uses credentials-work.json.
Tokens are stored per client, which is why the keyring key contains both the client and the email: token:default:you@example.com. When --client is absent, gog consults the account_clients and client_domains maps in the config, then a credentials file named after the email domain, and finally falls back to default.
Most readers need only the default client. A second client becomes useful when, for example, a Google Workspace organization requires its own Cloud project.
Where gog keeps its files
gog uses 4 directories. The separate data directory lets you sync configuration files as dotfiles without including credentials.
| Kind | Contents | Linux default | With GOG_HOME=/persist/gogcli |
|---|---|---|---|
| Config | config.json (aliases, keyring backend, client maps) | ~/.config/gogcli | /persist/gogcli/config |
| Data | credentials*.json, the file keyring, service-account keys | ~/.local/share/gogcli | /persist/gogcli/data |
| State | Gmail watch cursors, email tracking state | ~/.local/state/gogcli | /persist/gogcli/state |
| Cache | Intermediate files for Gmail backups | ~/.cache/gogcli | /persist/gogcli/cache |
For each kind, the first setting that exists wins: GOG_CONFIG_DIR, GOG_DATA_DIR, GOG_STATE_DIR or GOG_CACHE_DIR; then the global flag --home for one invocation; then GOG_HOME; then the matching XDG_*_HOME variable; then the platform default. GOG_HOME, --home and the per-kind variables must be absolute paths. On macOS and Windows, the defaults come from the operating system’s user configuration and cache directories instead of the XDG layout.
Use these 3 commands to find the most common paths. The command gog config path prints the config file, gog auth status prints the config path and the keyring backend, and gog auth doctor prints the file keyring directory. Installation and First Checks runs all 3 on a fresh GOG_HOME.