# Complete Guide: Setting Up gogcli - Google APIs in Your Terminal — Liran Tal

Set up gogcli for Google APIs and OpenClaw AI assistants, with OAuth credentials, granular scopes, multiple accounts, and remote VM authorization.

Canonical: https://lirantal.com/blog/gogcli-setup-guide-for-google-apis-and-openclaw-personal-assistant-ai

![Cover artwork for Complete Guide: Setting Up gogcli - Google APIs in Your Terminal](/_astro/gogcli-setup-guide-for-google-apis-and-openclaw-personal-assistant-ai.CBZCbnDx_9YNvz.webp)

**gogcli** (`gog`) is a unified command-line interface for Gmail, Calendar, Drive, Contacts, Tasks, Sheets, Docs, Slides, and other Google services. This guide covers installation, OAuth setup (including SSH forwarding for remote VMs), and how to configure granular permissions using OAuth scopes.

* * *

## Table of Contents

1.  [Installation](#installation)
2.  [Understanding OAuth Scopes](#understanding-oauth-scopes)
3.  [Setting Up OAuth Credentials](#setting-up-oauth-credentials)
4.  [Authorization Scenarios](#authorization-scenarios)
5.  [SSH Forwarding for Remote VMs](#ssh-forwarding-for-remote-vms)
6.  [Scope Configuration Options](#scope-configuration-options)
7.  [Managing Multiple Accounts](#managing-multiple-accounts)
8.  [Troubleshooting](#troubleshooting)

* * *

## Installation

### Homebrew (macOS/Linux)

```
brew install steipete/tap/gogcli
```

### Build from Source

```
git clone https://github.com/steipete/gogcli.git
cd gogcli
make
./bin/gog --help
```

### Verify Installation

```
gog --version
```

* * *

## Understanding OAuth Scopes

Google uses OAuth 2.0 scopes to control what your application can access. Unfortunately, **Google’s APIs don’t offer perfectly granular permissions** - you often get bundled capabilities.

### Gmail Scopes Matrix

Scope

Read Emails

Send Emails

Modify Labels

**Permanently Delete**

Settings

`gmail.readonly`

✅

❌

❌

❌

❌

`gmail.compose`

❌

✅ (compose only)

❌

❌

❌

`gmail.send`

❌

✅ (send only)

❌

❌

❌

**`gmail.modify`** (default)

✅

✅

✅

**❌**

❌

`gmail.settings.basic`

❌

❌

❌

❌

✅ (filters, labels)

`gmail.settings.sharing`

❌

❌

❌

❌

✅ (delegation, forwarding)

`mail.google.com/`

✅

✅

✅

**✅**

✅

**Default gogcli Gmail scopes:**

*   `gmail.modify` - Read, send, modify labels (no permanent delete)
*   `gmail.settings.basic` - Manage filters and label settings
*   `gmail.settings.sharing` - Manage delegation and forwarding

> 💡 **Key Insight:** The default `gmail.modify` scope does NOT allow permanent deletion. This is a safer alternative to the nuclear `mail.google.com/` scope.

### Calendar Scopes Matrix

Scope

Read Events

Create Events

Modify Events

Delete Events

Settings

`calendar.readonly`

✅

❌

❌

❌

❌

`calendar.events`

❌

✅

✅

✅

❌

**`calendar`** (default)

✅

✅

✅

✅

✅

**Default gogcli Calendar scope:** `calendar` (full access)

> ⚠️ **Limitation:** Google doesn’t offer a “read + create/modify without delete” scope for Calendar.

### Drive Scopes Matrix

Scope

Search/List

Download

Upload

Modify

Delete

Access All Files

`drive.readonly`

✅

✅

❌

❌

❌

✅

`drive.file`

✅ (limited)

✅

✅

✅

❌

❌ (app-created only)

**`drive`** (default)

✅

✅

✅

✅

✅

✅

**Default gogcli Drive scope:** `drive` (full access)

> 💡 **Useful Option:** `drive.file` limits access to files the app creates or opens, preventing access to your entire Drive.

### Other Service Scopes

Service

Default Scope

Read-Only Alternative

Notes

**Contacts**

`contacts`, `contacts.other.readonly`, `directory.readonly`

`contacts.readonly` + others

Directory scope is always read-only

**Tasks**

`tasks`

`tasks.readonly`

Full CRUD vs read-only

**Sheets**

`spreadsheets`, `drive`

`spreadsheets.readonly`, `drive.readonly`

Needs Drive for export

**Docs**

`documents`, `drive`

`documents.readonly`, `drive.readonly`

Needs Drive for export

**People**

`profile` (OIDC)

N/A

Already read-only

**Groups**

`cloud-identity.groups.readonly`

N/A

Workspace only, read-only

**Keep**

`keep.readonly`

N/A

Workspace only, read-only

* * *

## Setting Up OAuth Credentials

### Step 1: Create a Google Cloud Project

1.  Go to [Google Cloud Console](https://console.cloud.google.com/)
2.  Click **“Create Project”** or select an existing one
3.  Note your project name/ID

### Step 2: Enable Required APIs

Enable the APIs you need:

*   [Gmail API](https://console.cloud.google.com/apis/api/gmail.googleapis.com)
*   [Google Calendar API](https://console.cloud.google.com/apis/api/calendar-json.googleapis.com)
*   [Google Drive API](https://console.cloud.google.com/apis/api/drive.googleapis.com)
*   [Google Contacts (People API)](https://console.cloud.google.com/apis/api/people.googleapis.com)
*   [Google Tasks API](https://console.cloud.google.com/apis/api/tasks.googleapis.com)
*   [Google Sheets API](https://console.cloud.google.com/apis/api/sheets.googleapis.com)
*   [Cloud Identity API](https://console.cloud.google.com/apis/api/cloudidentity.googleapis.com) (for Groups)

### Step 3: Configure OAuth Consent Screen

1.  Navigate to: [APIs & Services → OAuth consent screen](https://console.cloud.google.com/apis/credentials/consent)
2.  Choose **“Internal”** (for Workspace) or **“External”** (for personal accounts)
3.  Fill in required fields:
    *   **App name:** “gogcli” (or your preferred name)
    *   **User support email:** Your email
    *   **Developer contact:** Your email
4.  Click **“Save and Continue”**
5.  **Scopes:** Skip for now (gogcli will request dynamically)
6.  **Test users** (if External): Add your email address
7.  Click **“Save and Continue”** → **“Back to Dashboard”**

### Step 4: Create OAuth Client Credentials

1.  Go to: [APIs & Services → Credentials](https://console.cloud.google.com/apis/credentials)
2.  Click **“Create Credentials”** → **“OAuth client ID”**
3.  Select **“Desktop app”** as the application type
4.  Name it: **“gogcli”**
5.  Click **“Create”**
6.  Click **“Download JSON”** and save to a secure location (e.g., `~/Downloads/client_secret_*.json`)

> ⚠️ **Security:** Never commit this file to version control or share it publicly!

### Step 5: Store Credentials in gogcli

```
gog auth credentials ~/Downloads/client_secret_*.json
```

This stores the OAuth client credentials in your user config directory:

*   **macOS:** `~/Library/Application Support/gogcli/credentials.json`
*   **Linux:** `~/.config/gogcli/credentials.json`
*   **Windows:** `%AppData%\gogcli\credentials.json`

* * *

## Authorization Scenarios

### Scenario 1: Local Machine (Simple)

This is the standard flow when running `gog` on your local laptop/desktop.

```
# Authorize with default scopes
gog auth add you@gmail.com
```

**What happens:**

1.  `gog` starts a local HTTP server on `http://127.0.0.1:PORT/oauth2/callback`
2.  Your browser opens Google’s OAuth consent screen
3.  You approve the requested permissions
4.  Google redirects to the local callback URL
5.  `gog` captures the authorization code and exchanges it for a refresh token
6.  The refresh token is stored securely in your OS keychain

**Default services authorized:** Gmail, Calendar, Drive, Docs, Contacts, Tasks, Sheets, People

### Scenario 2: Remote VM Without Browser

When running `gog` on a headless server/VM without a browser, use **manual mode**:

```
gog auth add you@gmail.com --manual
```

**What happens:**

1.  `gog` prints an authorization URL
2.  You copy this URL and open it in a browser **on your local machine**
3.  After approving, Google redirects to `http://localhost:1/?code=...` (which won’t load)
4.  Copy the **entire redirect URL** from your browser’s address bar
5.  Paste it into the terminal where `gog` is waiting

**Example:**

```
Visit this URL to authorize:
https://accounts.google.com/o/oauth2/auth?client_id=...

After authorizing, you'll be redirected to a localhost URL that won't load.
Copy the URL from your browser's address bar and paste it here.

Paste redirect URL (Enter or Ctrl-D): http://localhost:1/?code=4/0AY...&state=abc123
```

### Scenario 3: Remote VM With SSH Forwarding (Recommended)

This allows the standard browser flow while running `gog` on a remote VM.

#### How It Works

1.  `gog` listens on `http://127.0.0.1:PORT/oauth2/callback` on the **remote VM**
2.  SSH forwards that port from your **local machine** to the remote VM
3.  Google redirects to your **local** `127.0.0.1:PORT`, which tunnels to the VM
4.  The VM receives the callback and completes authorization

#### Step-by-Step Setup

**Step 1: Start SSH Port Forwarding**

On your **local machine**, open a terminal and create an SSH tunnel:

```
# Forward a specific port (e.g., 42737)
ssh -L 42737:127.0.0.1:42737 user@remote-vm-ip -N

# Example:
ssh -L 42737:127.0.0.1:42737 node@10.0.0.12 -N
```

*   `-L 42737:127.0.0.1:42737` - Forward local port 42737 to remote port 42737
*   `-N` - Don’t execute commands, just forward the port
*   Leave this terminal open during authorization

**Step 2: Authorize on the Remote VM**

In a **separate terminal**, SSH into your VM and run:

```
ssh user@remote-vm-ip

# On the remote VM:
gog auth add you@gmail.com
```

**What happens:**

1.  `gog` picks a random port (let’s say 54321) and listens on it
2.  Your local browser opens the OAuth consent page
3.  After approval, Google redirects to `http://127.0.0.1:54321/oauth2/callback`
4.  But wait! The ports don’t match! 😱

#### ⚠️ Problem: Random Port Selection

By default, `gog` picks a **random available port** for the callback server. This means you can’t pre-configure SSH forwarding.

#### 🔧 Workaround: Use Manual Mode with Local Browser

Since `gog` doesn’t support specifying a fixed callback port, the best approach is:

1.  **On your local machine:** Run the full auth flow
2.  **Export and import** the credentials to the remote VM

```
# On local machine (with browser):
gog auth add you@gmail.com
gog auth tokens export you@gmail.com > token.json

# Copy to remote VM:
scp token.json user@remote-vm-ip:~/

# On remote VM:
gog auth tokens import ~/token.json
rm ~/token.json  # Clean up
```

**Alternative:** Use `--manual` mode (see Scenario 2) - no SSH forwarding needed.

* * *

## Scope Configuration Options

### Default Services (Recommended)

Authorize with all user-facing services using default scopes:

```
gog auth add you@gmail.com --services user
```

**Equivalent to:**

```
gog auth add you@gmail.com --services gmail,calendar,drive,docs,contacts,tasks,sheets,people
```

**Scopes granted:**

*   Gmail: `gmail.modify`, `gmail.settings.basic`, `gmail.settings.sharing`
*   Calendar: `calendar`
*   Drive: `drive`
*   Docs: `documents`, `drive`
*   Contacts: `contacts`, `contacts.other.readonly`, `directory.readonly`
*   Tasks: `tasks`
*   Sheets: `spreadsheets`, `drive`
*   People: `profile`

### Limit to Specific Services

Only authorize services you need:

```
# Just Gmail
gog auth add you@gmail.com --services gmail

# Gmail and Calendar only
gog auth add you@gmail.com --services gmail,calendar

# Drive and Sheets
gog auth add you@gmail.com --services drive,sheets
```

### Read-Only Mode

Request read-only scopes where available:

```
gog auth add you@gmail.com --services gmail,calendar,drive --readonly
```

**Scopes granted:**

*   Gmail: `gmail.readonly`
*   Calendar: `calendar.readonly`
*   Drive: `drive.readonly`

**Limitations:**

*   ❌ Can’t send emails
*   ❌ Can’t create/modify calendar events
*   ❌ Can’t upload files to Drive

### Fine-Grained Drive Permissions

Control Drive access level:

```
# Full access (default)
gog auth add you@gmail.com --services drive --drive-scope full

# Read-only
gog auth add you@gmail.com --services drive --drive-scope readonly

# App-created files only (safer)
gog auth add you@gmail.com --services drive --drive-scope file
```

**Comparison:**

Mode

Search Existing Files

Download Existing

Upload New

Modify App Files

Delete

`full`

✅

✅

✅

✅

✅

`readonly`

✅

✅

❌

❌

❌

`file`

❌

❌

✅

✅

❌

> 💡 **Use Case for `file` mode:** If you only use `gog` to upload new files (not browse existing Drive), this limits exposure.

### Custom Scope Combinations

**Example 1: Gmail + Calendar + Limited Drive**

```
gog auth add you@gmail.com \
  --services gmail,calendar,drive \
  --drive-scope file
```

**Scopes:**

*   Gmail: `gmail.modify` (read, send, modify labels - no delete)
*   Calendar: `calendar` (full access)
*   Drive: `drive.file` (only app-created files)

**Example 2: Read-Only Everything Except Calendar**

```
# This won't work - you can't mix readonly and write!
# You'd need separate accounts (see below)
```

### Re-Authorizing with Different Scopes

If you need to change scopes later:

```
# Force a new consent screen to update scopes
gog auth add you@gmail.com \
  --services gmail,calendar,drive \
  --readonly \
  --force-consent
```

The `--force-consent` flag ensures Google shows the permission screen again and issues a new refresh token.

### Checking Current Scopes

```
# Show current account's services and scopes
gog auth status

# List all services and their default scopes
gog auth services

# Verify token is valid
gog auth list --check
```

* * *

## Managing Multiple Accounts

### Using Multiple Google Accounts

You can authorize multiple accounts with different permissions:

```
# Personal account with full access
gog auth add personal@gmail.com --services user

# Work account with limited access
gog auth add work@company.com --services gmail,calendar --readonly

# Bot account with Drive-only file scope
gog auth add bot@company.com --services drive --drive-scope file
```

### Setting a Default Account

```
# Environment variable (temporary)
export GOG_ACCOUNT=personal@gmail.com

# Add to your shell profile for persistence
echo 'export GOG_ACCOUNT=personal@gmail.com' >> ~/.bashrc
# or ~/.zshrc for zsh
```

### Switching Accounts Per Command

```
# Use specific account for one command
gog gmail search 'is:unread' --account work@company.com

# Default account (from GOG_ACCOUNT)
gog gmail search 'is:unread'
```

### Managing Stored Accounts

```
# List all authorized accounts
gog auth list

# Check which tokens are valid
gog auth list --check

# Remove an account
gog auth remove old@gmail.com

# Export/Import tokens (for backup or moving to another machine)
gog auth tokens export you@gmail.com > token-backup.json
gog auth tokens import token-backup.json
```

* * *

## Troubleshooting

### Issue: “Authorization required” Error

**Cause:** Token not found or expired.

**Solution:**

```
# Re-authorize
gog auth add you@gmail.com --force-consent

# Check token validity
gog auth list --check
```

### Issue: macOS Keychain Permission Prompts

**Cause:** macOS Keychain repeatedly asks for permission when binary path changes.

**Solutions:**

**Option 1: Use a stable binary path**

```
# Always use the same binary
sudo mv ./bin/gog /usr/local/bin/gog
```

**Option 2: Switch to file-based keyring**

```
# Configure file backend (encrypted on-disk)
gog auth keyring file

# For non-interactive (CI/scripts)
export GOG_KEYRING_PASSWORD='your-secure-password'
```

### Issue: SSH Forwarding Not Working

**Symptoms:** Browser opens, you authorize, but callback hangs.

**Diagnosis:**

1.  Check if SSH tunnel is active: `ps aux | grep ssh`
2.  Verify port forwarding: `netstat -an | grep 42737`
3.  Check for port conflicts: `lsof -i :42737`

**Solution:** Use manual mode instead:

```
gog auth add you@gmail.com --manual
```

### Issue: Insufficient Permissions (403 Error)

**Cause:** Your scopes don’t include the required permission.

**Example Error:**

```
Error: googleapi: Error 403: Request had insufficient authentication scopes.
```

**Solution:**

```
# Check current scopes
gog auth status

# Re-authorize with needed service
gog auth add you@gmail.com --services gmail,calendar,drive --force-consent
```

### Issue: “No Refresh Token” Error

**Cause:** Google didn’t return a refresh token (already granted scopes).

**Solution:** Force consent screen:

```
gog auth add you@gmail.com --services user --force-consent
```

### Issue: Headless Server - No Browser

**Solution:** Use manual mode:

```
gog auth add you@gmail.com --manual
```

### Issue: Rate Limiting (429 Error)

**Cause:** Too many API requests.

**Solution:** Wait and retry. `gog` has built-in retry logic for most operations.

* * *

## Best Practices

### ✅ Security Recommendations

1.  **Use default scopes** - The `gmail.modify` scope already prevents permanent deletion
2.  **Never commit credentials** - Add `credentials.json` and `config.json` to `.gitignore`
3.  **Use app passwords for automation** - For long-running services, consider service accounts (Workspace only)
4.  **Limit scope to what you need** - Don’t authorize services you won’t use
5.  **Regularly audit tokens** - Run `gog auth list --check` periodically

### ✅ Workflow Recommendations

1.  **Set `GOG_ACCOUNT` environment variable** - Avoid repeating `--account` flag
2.  **Use `--json` for scripting** - Parseable output for automation
3.  **Test with read-only first** - Validate your workflow before enabling write access
4.  **Use separate accounts for different purposes** - Personal vs. work vs. automation

### ✅ Remote VM Setup

1.  **Use manual mode** (`--manual` flag) - Simpler than SSH forwarding
2.  **Or authorize locally and copy token** - Export/import token JSON
3.  **Secure your keyring** - Use `GOG_KEYRING_PASSWORD` in CI/CD
4.  **Don’t use sudo** - Keep tokens in user keyring, not root

* * *

## Quick Reference Commands

```
# Installation
brew install steipete/tap/gogcli

# Setup OAuth credentials
gog auth credentials ~/Downloads/client_secret.json

# Authorize (local machine)
gog auth add you@gmail.com

# Authorize (remote VM)
gog auth add you@gmail.com --manual

# Authorize with limited scopes
gog auth add you@gmail.com --services gmail,calendar --readonly

# Authorize with custom Drive scope
gog auth add you@gmail.com --services drive --drive-scope file

# Check current scopes
gog auth status

# List available services and scopes
gog auth services

# Set default account
export GOG_ACCOUNT=you@gmail.com

# Use specific account
gog gmail search 'is:unread' --account work@example.com

# Re-authorize with force consent
gog auth add you@gmail.com --services user --force-consent

# Check token validity
gog auth list --check

# Remove account
gog auth remove old@gmail.com
```

* * *

## Conclusion

The **gogcli** tool provides a powerful way to interact with Google services from the command line. While Google’s OAuth scopes don’t offer perfectly granular permissions, you can achieve reasonable security by:

1.  Using **default scopes** (which already exclude permanent deletion for Gmail)
2.  Enabling **`--readonly` mode** when you only need read access
3.  Using **`--drive-scope file`** to limit Drive access
4.  Managing **multiple accounts** with different permission levels

For remote VM scenarios, **manual mode** (`--manual`) is the most reliable approach, though SSH forwarding can work with the export/import token workflow.

**Project Repository:** [github.com/steipete/gogcli](https://github.com/steipete/gogcli)

**Documentation:** [gogcli.sh](https://gogcli.sh)

## More writing

[All writing](/blog)

*   Nov 23, 2026

    ### [5 Steps to Refactor a Fastify App for Testing and Growth](/blog/refactor-fastify-app-testing-growth)

*   Nov 14, 2026

    ### [Every Security Weakness Matters When Vulnerability Chaining is King](/blog/every-security-weakness-matters-vulnerability-chaining)

*   Sep 5, 2026

    ### [Self-Hosting Hermes Agent with Matrix E2EE on Proxmox](/blog/self-host-hermes-agent-matrix-e2ee-proxmox)


* * *

Liran Tal is the author of the book [Node.js Secure Coding: Defending Against Command Injection Vulnerabilities](https://www.nodejs-security.com/)

[Start Building Secure Node.js Applications](https://www.nodejs-security.com/)

 [![](/_astro/nodejs-secure-coding-book-3d-style.CGE-cDF3_1NNN99.png)](https://www.nodejs-security.com/)
