Google Workspace BYoK (Bring Your Own Key)
Direct Google Calendar and Gmail API access using your own GCP project OAuth2 credentials. Supports multiple Google accounts.
Prerequisites
- - Node.js (v18+)
- A Google Cloud project with Calendar and Gmail APIs enabled
- OAuth2 Desktop app credentials from your GCP project
Setup
Step 1: Install Dependencies
CODEBLOCK0
This installs googleapis (Google API client) and mupdf (PDF text extraction for email attachments).
Step 2: Create a Google Cloud Project
- 1. Go to Google Cloud Console and create a new project (or use an existing one)
- Enable the Google Calendar API and Gmail API:
- Go to
APIs & Services → Library
- Search for "Google Calendar API" → click
Enable
- Search for "Gmail API" → click
Enable
Step 3: Configure the OAuth Consent Screen
- 1. Go to Google Auth Platform → Audience (direct link)
- If prompted, configure the consent screen:
-
App name: anything (e.g., "OpenClaw")
-
User support email: your email
-
Scopes: skip (the auth script requests scopes at runtime)
- 3. If your app is in Testing publishing status (the default), add every Google account you want to authorize as a test user:
- Under
Test users, click
Add users
- Enter the email addresses of each account you'll connect
- Save
⚠️ Important: Apps in "Testing" status have a 7-day token expiry. To get long-lived tokens, publish your app to "Production" in the Audience settings. For personal Gmail accounts (External user type), you can skip Google's verification review — you'll just see an "unverified app" warning during consent. This is fine for personal use.
Step 4: Create OAuth Credentials
- 1. Go to Google Auth Platform → Clients (direct link)
- Click Create Client → choose Desktop app as the application type
- Name it whatever you like (e.g., "OpenClaw")
- Click Create and download the credentials JSON
- Run the setup script:
CODEBLOCK1
This copies your credentials to ~/.openclaw/google-workspace-byok/credentials.json.
Step 5: Authorize Google Accounts
For each Google account you want to connect:
CODEBLOCK2
The <label> is a friendly name you'll use to reference this account (e.g., "personal", "work", "household").
Auth flow:
- 1. The script prints an authorization URL
- Open the URL in your browser and sign in with the Google account
- Grant the requested permissions
- You'll be redirected to
http://localhost/... — the page won't load, and that's expected - Copy the full URL from your browser's address bar and paste it back into the script
- The script exchanges the code for tokens and saves them
Scopes requested (default — read/write):
- -
calendar — Full read/write access to Google Calendar - INLINECODE6 — Read-only access to Gmail
Pass --readonly to request read-only calendar access instead.
Tokens are stored in ~/.openclaw/google-workspace-byok/tokens/<label>.json.
Usage
All scripts are in {baseDir}/scripts/. Run them with node.
Calendar
CODEBLOCK3
Gmail
CODEBLOCK4
Gmail search uses the same query syntax as the Gmail web search box (e.g., is:unread, from:, newer_than:1d, has:attachment).
Reading PDF Attachments
The skill includes mupdf for extracting text from PDF attachments — useful for newsletters, invoices, school letters, etc. It handles multilingual text (Japanese, Chinese, etc.) well.
CODEBLOCK5
Note: mupdf is an ESM module — use node --input-type=module with import syntax, not require().
Account Management
CODEBLOCK6
File Layout
CODEBLOCK7
Troubleshooting
Error 403: access_denied — "has not completed the Google verification process"
Your app is in
Testing mode and the Google account isn't listed as a test user. Fix:
Google Auth Platform → Audience → Test users → Add the email.
Error: invalid_grant
The refresh token expired or was revoked. Re-run
node {baseDir}/scripts/auth.js --account <label> to re-authorize.
Tokens expire after 7 days
Apps in "Testing" publishing status issue tokens that expire after 7 days. Publish your app to "Production" for long-lived tokens. For personal Gmail (External user type), you can skip verification and just accept the "unverified app" warning.
Error: redirect_uri_mismatch
Your credentials.json doesn't include
http://localhost as a redirect URI. Edit your OAuth client in GCP Console →
Authorized redirect URIs → add
http://localhost.
npm install fails or mupdf won't install
mupdf requires a C++ build toolchain on some platforms. If it fails, you can still use all other features — PDF text extraction is the only feature that requires it. Try:
npm install --ignore-scripts to skip native compilation, then install
mupdf separately if needed.
Google Workspace BYoK(自带密钥)
使用您自己的GCP项目OAuth2凭据直接访问Google日历和Gmail API。支持多个Google账号。
前提条件
- - Node.js(v18+)
- 已启用日历和Gmail API的Google Cloud项目
- 来自GCP项目的OAuth2 桌面应用凭据
设置
步骤1:安装依赖
bash
cd {baseDir}/scripts && npm install
这将安装googleapis(Google API客户端)和mupdf(用于邮件附件的PDF文本提取)。
步骤2:创建Google Cloud项目
- 1. 前往Google Cloud控制台并创建一个新项目(或使用现有项目)
- 启用Google Calendar API和Gmail API:
- 前往
API和服务 → 库
- 搜索Google Calendar API → 点击
启用
- 搜索Gmail API → 点击
启用
步骤3:配置OAuth同意屏幕
- 1. 前往Google Auth平台 → 受众群体(直接链接)
- 如果提示,配置同意屏幕:
-
应用名称:任意名称(例如OpenClaw)
-
用户支持邮箱:您的邮箱
-
范围:跳过(认证脚本在运行时请求范围)
- 3. 如果您的应用处于测试发布状态(默认),请将您要授权的每个Google账号添加为测试用户:
- 在
测试用户下,点击
添加用户
- 输入您将连接的每个账号的邮箱地址
- 保存
⚠️ 重要提示: 处于测试状态的应用具有7天令牌有效期。要获取长期令牌,请在受众群体设置中将您的应用发布为正式版。对于个人Gmail账号(外部用户类型),您可以跳过Google的验证审核——在同意过程中您只会看到未验证应用警告。这对个人使用没有问题。
步骤4:创建OAuth凭据
- 1. 前往Google Auth平台 → 客户端(直接链接)
- 点击创建客户端 → 选择桌面应用作为应用类型
- 任意命名(例如OpenClaw)
- 点击创建并下载凭据JSON
- 运行设置脚本:
bash
node {baseDir}/scripts/setup.js --credentials /path/to/downloaded-credentials.json
这将把您的凭据复制到~/.openclaw/google-workspace-byok/credentials.json。
步骤5:授权Google账号
对于您要连接的每个Google账号:
bash
node {baseDir}/scripts/auth.js --account <标签>
<标签>是您将用来引用此账号的友好名称(例如personal、work、household)。
认证流程:
- 1. 脚本打印一个授权URL
- 在浏览器中打开该URL并使用Google账号登录
- 授予请求的权限
- 您将被重定向到http://localhost/...——页面不会加载,这是正常现象
- 从浏览器地址栏复制完整URL并粘贴回脚本
- 脚本将代码交换为令牌并保存
请求的范围(默认——读/写):
- - calendar — Google日历的完全读/写访问权限
- gmail.readonly — Gmail的只读访问权限
传递--readonly以请求只读日历访问权限。
令牌存储在~/.openclaw/google-workspace-byok/tokens/<标签>.json中。
使用方法
所有脚本都在{baseDir}/scripts/中。使用node运行它们。
日历
bash
列出所有日历
node {baseDir}/scripts/calendar.js --account <标签> --action list-calendars
列出即将到来的事件(默认:未来7天,主日历)
node {baseDir}/scripts/calendar.js --account <标签> --action events
带选项列出事件
node {baseDir}/scripts/calendar.js --account <标签> --action events --calendar
--days <数字> --max <数字>
获取特定事件
node {baseDir}/scripts/calendar.js --account <标签> --action get-event --calendar --event-id
检查空闲/忙碌
node {baseDir}/scripts/calendar.js --account <标签> --action freebusy --days <数字>
Gmail
bash
列出最近的邮件(默认:10封)
node {baseDir}/scripts/gmail.js --account <标签> --action list
搜索邮件
node {baseDir}/scripts/gmail.js --account <标签> --action list --query from:someone@example.com --max 20
读取特定邮件(包含带ID的附件元数据)
node {baseDir}/scripts/gmail.js --account <标签> --action read --message-id
下载邮件中的所有附件
node {baseDir}/scripts/gmail.js --account <标签> --action attachment --message-id --out-dir /tmp/attachments
下载特定附件
node {baseDir}/scripts/gmail.js --account <标签> --action attachment --message-id --attachment-id --out-dir /tmp
列出标签
node {baseDir}/scripts/gmail.js --account <标签> --action labels
Gmail搜索使用与Gmail网页搜索框相同的查询语法(例如is:unread、from:、newer_than:1d、has:attachment)。
读取PDF附件
该技能包含mupdf用于从PDF附件中提取文本——适用于新闻简报、发票、学校信件等。它能够很好地处理多语言文本(日语、中文等)。
bash
1. 下载附件
mkdir -p /tmp/attachments
node {baseDir}/scripts/gmail.js --account <标签> --action attachment --message-id --out-dir /tmp/attachments
2. 从PDF中提取文本
node --input-type=module -e
import * as mupdf from {baseDir}/scripts/node_modules/mupdf/dist/mupdf.js;
import fs from fs;
const data = fs.readFileSync(/tmp/attachments/filename.pdf);
const doc = mupdf.Document.openDocument(data, application/pdf);
for (let i = 0; i < doc.countPages(); i++) {
const page = doc.loadPage(i);
console.log(page.toStructuredText(preserve-whitespace).asText());
}
注意: mupdf是一个ESM模块——使用node --input-type=module配合import语法,而不是require()。
账号管理
bash
列出已配置的账号
node {baseDir}/scripts/accounts.js --action list
检查令牌状态
node {baseDir}/scripts/accounts.js --action status --account <标签>
文件结构
~/.openclaw/google-workspace-byok/
├── credentials.json # 您的GCP OAuth凭据
└── tokens/
├── personal.json # personal账号的令牌
└── work.json # work账号的令牌
故障排除
Error 403: access_denied — 尚未完成Google验证过程
您的应用处于测试模式,且该Google账号未列为测试用户。解决方法:前往Google Auth平台 → 受众群体 → 测试用户 → 添加该邮箱。
Error: invalid_grant
刷新令牌已过期或被撤销。重新运行node {baseDir}/scripts/auth.js --account <标签>以重新授权。
令牌在7天后过期
处于测试发布状态的应用颁发的令牌在7天后过期。将您的应用发布为正式版以获取长期令牌。对于个人Gmail(外部用户类型),您可以跳过验证,只需接受未验证应用警告。
Error: redirecturimismatch
您的credentials.json不包含http://localhost作为重定向URI。在GCP控制台中编辑您的OAuth客户端 → 授权重定向URI → 添加http://localhost。
npm install失败或mupdf无法安装
mupdf在某些平台上需要C++构建工具链。如果安装失败,您仍然可以使用所有其他功能——PDF文本提取是唯一需要它的功能。尝试:npm install --ignore-scripts以跳过原生编译,然后根据需要单独安装mupdf。