Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Authentication and Accounts

Set up gog to use a Google account, then choose read-only access, add a second account, or authorize a headless machine as needed. This chapter also explains how to diagnose common authentication failures and remove an account from gog.

How gog Talks to Google explains Cloud projects, OAuth clients, consent screens, scopes, tokens, and keyrings.

The starting point

On a fresh installation, any command that reads Google data fails before it reaches the network:

$ gog --account you@example.com gmail search 'is:unread'
OAuth client credentials missing (OAuth client ID JSON).
Download from: https://console.cloud.google.com/apis/credentials (Create Credentials → OAuth client ID → Desktop app → Download JSON)
Then run: gog auth credentials <credentials.json> (expected at /tmp/tmp.EfL8KEdyac/data/credentials.json)

The exit code is 10, which the exit-code table in gog --help calls config. gog --account you@example.com calendar events --today fails with the same message. With --json, gog prints the message to stderr as before and also writes an error object to stdout, which a script can parse:

{"error":{"class":"config","code":10,"message":"OAuth client credentials missing (OAuth client ID JSON).\nDownload from: https://console.cloud.google.com/apis/credentials (Create Credentials → OAuth client ID → Desktop app → Download JSON)\nThen run: gog auth credentials <credentials.json> (expected at /tmp/tmp.EfL8KEdyac/data/credentials.json)"}}

The message already outlines the procedure: obtain a Desktop app OAuth client, store it with gog auth credentials, then authorize an account.

About the outputs in this chapter

All outputs in this chapter come from real runs of gog v0.48.1 in a throwaway home with the file keyring, set up like this:

export GOG_HOME=$(mktemp -d)
export DBUS_SESSION_BUS_ADDRESS=disabled:
export GOG_KEYRING_BACKEND=file
export GOG_KEYRING_PASSWORD=demo-password

The test machine has no real OAuth client and cannot sign in to Google. It used a placeholder client file with the client ID 123456789012-demo.apps.googleusercontent.com, and it created 2 accounts with gog auth import and a placeholder refresh token (see Headless and SSH machines). Every local command behaves as it would with real credentials; only commands that contact Google, such as gog auth list --check, would fail.

Step 1: Create the Cloud project and the OAuth client

This part happens in the Google Cloud console, once for each OAuth client. These steps use the same console pages as the gog quickstart and gog auth setup.

  1. Create a project at https://console.cloud.google.com/projectcreate. A project ID must have 6 to 30 characters: lowercase letters, digits and hyphens, starting with a letter. This chapter uses my-gog-project.
  2. Enable the APIs in the API library: the Gmail API and the Google Calendar API.
  3. Configure the consent screen at https://console.cloud.google.com/auth/branding. Choose the user type External and enter your own address as the contact.
  4. Publish the app on the Audience page with “Publish app” and “Confirm”, so that refresh tokens do not expire after 7 days. How gog Talks to Google explains why.
  5. At https://console.cloud.google.com/auth/clients, add a client of type Desktop app. Download the JSON file Google provides. Its name usually starts with client_secret_.

Store the downloaded file securely because it contains the client secret.

Step 2 (optional): Let gog auth setup guide you

gog auth setup turns the console steps into a checklist for your project and services. Without the Google Cloud CLI (gcloud) installed, it changes nothing and prints the remaining steps:

$ gog auth setup you@example.com --gcloud-project my-gog-project --services gmail,calendar
status	guided
project	my-gog-project
services	gmail,calendar
apis	calendar-json.googleapis.com,gmail.googleapis.com
next_1	Install gcloud, or create/select a Google Cloud project manually
next_2	Configure OAuth consent: https://console.cloud.google.com/auth/branding?project=my-gog-project
next_3	Create a Desktop OAuth client and download its JSON: https://console.cloud.google.com/auth/clients?project=my-gog-project
next_4	Store the download with `gog auth credentials /path/to/client_secret.json`
next_5	Authorize with `gog auth add you@example.com --services gmail,calendar`
next_6	Verify with `gog auth doctor --check`

When gcloud is installed and authenticated, these flags automate parts of the setup (auth_setup.go):

FlagEffect
--create-projectCreates the project named by --gcloud-project, after asking for confirmation
--enable-apisEnables the APIs of the selected services with gcloud services enable
--open-consoleOpens the OAuth client page of the project in the browser
--credentials <file>Stores the downloaded client JSON, like gog auth credentials
--loginContinues with the browser authorization of the given account

The consent screen and the Desktop OAuth client still have to be created in the console. Add --dry-run to see the plan without changing anything; the plan lists the API identifiers that --enable-apis would enable, here calendar-json.googleapis.com and gmail.googleapis.com.

Step 3: Store the OAuth client

Give gog the downloaded file:

gog auth credentials ~/Downloads/client_secret_*.json

gog auth credentials <file> is a shortcut for gog auth credentials set <file>. Pass - to read the JSON from standard input. In the test home, with the placeholder client file in the current directory:

$ gog auth credentials client_secret.json
path	/tmp/tmp.EfL8KEdyac/data/credentials.json
client	default
client_secret_in_keyring	true
$ gog auth credentials list
CLIENT   PATH                                       SECRET_KEYRING  DOMAINS
default  /tmp/tmp.EfL8KEdyac/data/credentials.json  true
$ cat "$GOG_HOME/data/credentials.json"
{
  "client_id": "123456789012-demo.apps.googleusercontent.com"
}

The stored credentials.json contains only the client ID. gog stores the client secret in the keyring. After this step you can delete or archive the downloaded file.

Step 4: Authorize an account

Authorize your account for the 2 services this book uses.

gog auth add you@example.com --services gmail,calendar

gog starts a temporary listener on a loopback address, opens the consent page in your browser, and waits (quickstart). You sign in as you@example.com, review the requested permissions and approve them.

Google then redirects the browser back to the listener, and gog stores the resulting refresh token in the keyring. The address must belong to a Google account; a mailbox at another provider cannot complete Google’s sign-in.

The top-level command gog login you@example.com is a shortcut for gog auth add.

Preview the request

Before any browser opens, --dry-run shows exactly what gog would request:

$ gog --dry-run auth add you@example.com --services gmail,calendar
Dry run: would auth.add
{
  "client": "default",
  "drive_scope": "full",
  "email": "you@example.com",
  "extra_scopes": null,
  "force_consent": false,
  "gmail_scope": "full",
  "listen_addr": "",
  "manual": false,
  "readonly": false,
  "redirect_host": "",
  "redirect_uri": "",
  "remote": false,
  "scopes": [
    "email",
    "https://www.googleapis.com/auth/calendar",
    "https://www.googleapis.com/auth/gmail.modify",
    "https://www.googleapis.com/auth/gmail.settings.basic",
    "https://www.googleapis.com/auth/gmail.settings.sharing",
    "https://www.googleapis.com/auth/userinfo.email",
    "openid"
  ],
  "services": [
    "gmail",
    "calendar"
  ],
  "step": 0
}

Read-only and narrower Gmail access

For a script or agent that only reads Google data, authorize the account with read-only scopes. Put the global flag --readonly before auth add:

$ gog --readonly --dry-run --json auth add you@example.com --services gmail,calendar | jq '.request.scopes'
[
  "email",
  "https://www.googleapis.com/auth/calendar.readonly",
  "https://www.googleapis.com/auth/gmail.readonly",
  "https://www.googleapis.com/auth/userinfo.email",
  "openid"
]

For Gmail alone, --gmail-scope offers finer modes. These are the scope lists gog printed for each mode with --services gmail:

--gmail-scopeGmail scopes requested
full (default)gmail.modify, gmail.settings.basic, gmail.settings.sharing
readonlygmail.readonly
sendgmail.send
read-sendgmail.readonly, gmail.send

The scope limit applies to every program that uses the token. Combine it with the runtime controls in Safety Controls for Scripts and Agents for defense in depth.

Changing the scopes of an existing account

Run gog auth add again for the same account with the complete set of services and modes you want, and add --force-consent so that Google shows the consent screen and issues a new refresh token. A bare --force-consent without --services falls back to the default service set, so check what the account has first with gog --json auth list, which records the services of each token.

Inspect what you have

The listings in this section already show the second account, you@example.org, and the aliases me and work, which the next 2 sections create.

Supported services

gog auth services prints every service with its scopes and APIs. The JSON form is easier to filter:

$ gog --json auth services | jq '.services[] | select(.service == "gmail" or .service == "calendar")'
{
  "service": "gmail",
  "user": true,
  "scopes": [
    "https://www.googleapis.com/auth/gmail.modify",
    "https://www.googleapis.com/auth/gmail.settings.basic",
    "https://www.googleapis.com/auth/gmail.settings.sharing"
  ],
  "apis": [
    "Gmail API"
  ]
}
{
  "service": "calendar",
  "user": true,
  "scopes": [
    "https://www.googleapis.com/auth/calendar"
  ],
  "apis": [
    "Calendar API"
  ]
}

user: true marks the services that the default --services user includes.

Stored accounts

gog auth list prints one line per stored token: the email, the OAuth client, the recorded services, the creation time and the kind of authentication.

$ gog auth list
you@example.com	default	gmail,calendar	2026-11-28T02:17:15Z	oauth
you@example.org	default	gmail,calendar	2026-11-28T02:17:15Z	oauth
$ gog auth tokens list
token:default:you@example.com
token:default:you@example.org

gog auth tokens list shows the keyring keys, which follow the pattern token:<client>:<email>. Add --check to gog auth list to exchange each refresh token with Google and report the ones that no longer work.

Status and diagnosis

gog auth status reports the configuration and, when an account is selected, the account’s client and credential files:

$ GOG_ACCOUNT=work gog auth status
config_path	/tmp/tmp.EfL8KEdyac/config/config.json
config_exists	true
keyring_backend	file
keyring_backend_source	env
account	you@example.org
client	default
credentials_path	/tmp/tmp.EfL8KEdyac/data/credentials.json
credentials_exists	true
client_secret_in_keyring	true
auth_preferred	oauth
service_account_configured	false

gog auth doctor walks through the configuration, the keyring and every token, and gives a hint for each problem:

$ gog auth doctor
ok	config.path	/tmp/tmp.EfL8KEdyac/config/config.json
ok	keyring.backend	file (source: env)
ok	keyring.dir	/tmp/tmp.EfL8KEdyac/data/keyring
ok	keyring.password	GOG_KEYRING_PASSWORD is set
hint	keyring.password	keep this value identical across shell, service, and agent configs
ok	keyring.open	opened
ok	tokens	2 readable OAuth tokens of 2 stored token accounts
ok	credentials.default	OAuth client credentials available
status	ok

With --check, doctor also exchanges every refresh token with Google, which is the real test after gog auth add. In scripts, read the status field of gog --json auth doctor --check; the command exits with 0 even when it reports errors.

Aliases and GOG_ACCOUNT

Typing full addresses in every command is tedious, and hard-coding them in scripts spreads personal data. An alias gives each account a short, stable name:

$ gog auth alias set me you@example.com
alias	me
email	you@example.com
$ gog auth alias set work you@example.org
alias	work
email	you@example.org
$ gog auth alias list
ALIAS  EMAIL
me     you@example.com
work   you@example.org
$ cat "$GOG_HOME/config/config.json"
{
  "account_aliases": {
    "me": "you@example.com",
    "work": "you@example.org"
  }
}

gog auth alias unset <alias> removes one. The names auto and default are reserved (auth_alias.go):

$ gog auth alias set auto you@example.com
alias name is reserved

An alias works anywhere an account is expected: gog --account work gmail search 'is:unread', or GOG_ACCOUNT=me in the environment. With 2 accounts stored and neither --account nor GOG_ACCOUNT set, gog refuses to guess:

$ gog gmail search 'is:unread'
missing --account (or set GOG_ACCOUNT, set default via `gog auth manage`, or store exactly one token)

The message lists all 4 sources in precedence order. gog auth manage (also reachable as gog auth login) opens a local account manager in the browser, where you can mark one account as the default.

For an interactive shell, export GOG_ACCOUNT=me sets the account for the session. In scripts, pass --account so each command specifies the account it uses.

A second account

Adding another account repeats Step 4 with a different address:

gog auth add you@example.org --services gmail,calendar
gog auth alias set work you@example.org
gog --account work calendar events --today

Both accounts use the default OAuth client unless you select another one. When an account should go through a different Cloud project, for example one that a Google Workspace organization manages, store that project’s client under a name of its own and map the account’s domain to it:

$ gog --client work auth credentials client_secret.json --domain example.org
path	/tmp/tmp.NDzVqBPFR7/data/credentials-work.json
client	work
client_secret_in_keyring	true
$ gog auth credentials list
CLIENT   PATH                                            SECRET_KEYRING  DOMAINS
default  /tmp/tmp.NDzVqBPFR7/data/credentials.json       true
work     /tmp/tmp.NDzVqBPFR7/data/credentials-work.json  true            example.org
$ gog --dry-run --json auth add you@example.org --services gmail,calendar | jq -r '.request.client'
work

The --domain option writes client_domains into config.json, so every @example.org account now selects the work client without --client. This listing comes from a separate home that held only those 2 clients. The OAuth clients guide gives the full resolution order.

Set up the domain mapping before authorizing accounts in that domain. From the moment the mapping exists, gog resolves you@example.org to the work client. In a test where the token had been stored under default first, gog --force auth remove you@example.org afterward reported client work and left the default token in place.

Headless and SSH machines

A server, a container or a remote shell usually has no browser, and often no desktop keyring. gog supports 4 ways to set up authentication on these machines.

The manual flow

gog auth add you@example.com --services gmail,calendar --manual

gog prints an authorization URL to stderr. Open it in a browser on any machine and approve the request.

Google then redirects to a loopback URL, which fails to load because no listener runs on that machine; copy the full URL from the address bar and paste it at gog’s prompt, Paste redirect URL (Enter or Ctrl-D): (oauth_flow_manual.go). Manual flows wait 5 minutes by default, which --timeout changes.

The 2-step remote flow

When the command cannot stay open while you switch to a browser (for example, in an automation that runs one command at a time), split the flow:

$ gog auth add you@example.com --services gmail,calendar --remote --step 1
Google sign-in requires a Google Account or Google Workspace account; if consent fails, retry with explicit --services and preserve existing services when reauthorizing.
auth_url	https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=...
state_reused	false
Run again with the same root flags and --remote --step 2 --auth-url <redirect-url> --services gmail,calendar

The auth_url line is shortened here. Open the full URL, approve, copy the redirect URL from the address bar, and finish with:

gog auth add you@example.com --services gmail,calendar --remote --step 2 --auth-url '<redirect-url>'

Step 1 saves a short-lived verifier in the config directory, so step 2 must run with the same GOG_HOME and the same --client (OAuth clients guide). When the browser can reach the server directly, --listen-addr, --redirect-host and --redirect-uri let you choose where gog listens and which callback URL it registers.

Moving or importing a refresh token

If the account is already authorized on another machine, you can copy its refresh token without another sign-in.

$ gog auth tokens export you@example.com --out exported-token.json
WARNING: exported file contains OAuth tokens (keep it safe and delete it when done)
exported	true
email	you@example.com
client	default
path	exported-token.json
$ jq keys exported-token.json
[
  "client",
  "created_at",
  "email",
  "refresh_token",
  "services"
]

On the target machine, store the same OAuth client first, then import the file:

$ gog auth credentials client_secret.json
path	/tmp/tmp.AJhJ1RnTLe/data/credentials.json
client	default
client_secret_in_keyring	true
$ gog auth tokens import exported-token.json
Imported refresh token into keyring
imported	true
email	you@example.com
client	default
$ gog auth list
you@example.com	default	gmail,calendar	2026-11-28T02:17:15Z	oauth

gog needs the client ID and secret of the client named in the file to exchange the refresh token, which is why the client file comes first. Delete the exported file as soon as the import succeeds.

Use gog auth import to load a refresh token from a secret manager. Exactly one of --refresh-token-env, --refresh-token-file and --refresh-token-stdin supplies it:

$ gog auth import --email you@example.com --refresh-token-env DEMO_REFRESH_TOKEN --services gmail,calendar
imported	true
client	default
email	you@example.com

gog auth list displays the services recorded by --services. The token retains the scopes it was originally granted. gog auth import refuses to overwrite an existing entry unless you add --force.

The file keyring and its password

Without a desktop session there is no Secret Service, and the file keyring takes its place. You can select it explicitly in the environment or once in the configuration:

$ gog auth keyring file
NOTE: file keyring backend in non-interactive context requires GOG_KEYRING_PASSWORD
written	true
path	/tmp/tmp.34PorqgVFD/config/config.json
keyring_backend	file

This output comes from another throwaway home in which GOG_KEYRING_BACKEND was unset; when the variable is set, gog adds a note that it overrides config.json. Every process that runs gog must receive the same GOG_KEYRING_PASSWORD: your shell, a systemd service, a CI job and an agent’s subprocesses. When the variable is missing, doctor shows the failure clearly:

$ env -u GOG_KEYRING_PASSWORD gog auth doctor
ok	config.path	/tmp/tmp.EfL8KEdyac/config/config.json
ok	keyring.backend	file (source: env)
ok	keyring.dir	/tmp/tmp.EfL8KEdyac/data/keyring
error	keyring.password	file keyring selected but GOG_KEYRING_PASSWORD is not set in a non-interactive process
hint	keyring.password	set GOG_KEYRING_PASSWORD or switch to a system keyring
ok	keyring.open	opened
error	token.default.you@example.com	read token: read encoded file keyring item: no TTY available for keyring file backend password prompt; set GOG_KEYRING_PASSWORD
hint	token.default.you@example.com	file keyring needs GOG_KEYRING_PASSWORD in non-interactive shells, services, CI, and agents
error	token.default.you@example.org	read token: read encoded file keyring item: no TTY available for keyring file backend password prompt; set GOG_KEYRING_PASSWORD
hint	token.default.you@example.org	file keyring needs GOG_KEYRING_PASSWORD in non-interactive shells, services, CI, and agents
ok	tokens	0 readable OAuth tokens of 2 stored token accounts
status	error

A minimal environment for a headless service therefore contains GOG_HOME, GOG_KEYRING_BACKEND=file and GOG_KEYRING_PASSWORD, the last one injected from a secret store and never written into a script or an image (install guide). Test from the same entry point that the service uses, because a command that works in your login shell proves only that your shell has the password. To change the password later, gog auth rekey writes a re-encrypted copy of the keyring, as described in the paths guide.

Removing an account

gog auth remove deletes an account’s token. It is a destructive command, so in a non-interactive shell it asks for --force, and --dry-run shows what it would do:

$ gog auth remove you@example.org
refusing to remove stored token for you@example.org without --force (non-interactive)
$ gog --dry-run auth remove you@example.org
Dry run: would auth.remove
{
  "email": "you@example.org"
}
$ gog --force auth remove you@example.org
deleted	true
email	you@example.org
client	default
$ gog auth alias list
ALIAS  EMAIL
me     you@example.com
$ gog auth list
you@example.com	default	gmail,calendar	2026-11-28T02:17:15Z	oauth

Along with the token, gog removed the alias work, which pointed to the removed account, and the account’s client mapping in config.json (auth_accounts.go). The top-level gog logout <email> is a shortcut for the same command.

gog auth remove deletes the local token. To revoke the OAuth grant, withdraw access in your Google Account. To remove the OAuth client from gog as well, run gog auth credentials remove (the default client) or gog auth credentials remove work for a named client.