Files
nexus/Skills/GOG-CLI-安装配置指南.md

286 lines
5.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 输出: /opt/homebrew/bin/gog
source:
author: shenwei
published:
created:
description:
tags: [gog, gog-cli, macos]
---
#gog #gog-cli #macos
本文档记录在 macOS 系统上安装和配置 gog CLI 的完整步骤,以便通过命令行管理 Google WorkspaceGmail、Google Calendar、Google Drive、Google Contacts、Google Docs、Google Sheets
## 目录
```table-of-contents
```
---
## 前置条件
- macOS 系统
- Homebrew 已安装
- Google 账号
---
## 安装步骤
### 1. 安装 gog CLI
使用 Homebrew 安装 gog CLI
```bash
brew install steipete/tap/gogcli
```
验证安装:
```bash
which gog
# 输出: /opt/homebrew/bin/gog
```
---
## 配置 OAuth 凭证
### 1. 在 Google Cloud Console 创建 OAuth 凭证
1. 打开 [Google Cloud Console - Credentials](https://console.cloud.google.com/apis/credentials)
2. 点击 **「创建凭证」** → 选择 **「OAuth 客户端 ID」**
3. 应用类型选择 **「桌面应用」**
4. 命名(例如:`gogcli`
5. 点击 **「创建」**
6. 点击 **「下载 JSON」**,得到 `credentials.json` 文件
### 2. 移动凭证文件到 gogcli 配置目录
- 创建 gogcli 配置目录(如果不存在):
```bash
mkdir -p "/Users/weishen/Library/Application Support/gogcli"
```
- 移动下载的凭证文件:
```bash
mv ~/Downloads/credentials.json "/Users/weishen/Library/Application Support/gogcli/credentials.json"
```
- 使用命令指定凭证路径:
```bash
gog auth credentials /Users/weishen/Library/Application\ Support/gogcli/credentials.json
```
---
## 解除 Google 安全限制
### 问题描述
首次授权时Google 会显示以下错误:
> 此应用未经 Google 验证
> 此应用请求访问您 Google 账号中的敏感信息。在开发者让该应用通过 Google 验证之前,请勿使用该应用。
### 解决方法:添加测试用户
1. 打开 [Google Cloud Console - Credentials](https://console.cloud.google.com/apis/credentials)
2. 找到你创建的 OAuth 客户端ID的项目点击进入详情
3. 找到 **「目标对象」**
4. 找到 **「测试用户」** 部分
5. 点击 **「添加用户」**
6. 输入你的 Google 邮箱:`ishenwei@gmail.com`
7. 保存
添加测试用户后,重新运行授权命令即可:
```bash
gog auth add ishenwei@gmail.com --services gmail,calendar,drive,contacts,docs,sheets
```
这会打开浏览器让你登录 Google 账号并授权。
---
## 启用Google API 服务(以 Gmail API 为例)
### 1. 核心原理
Google API 调用需要满足两层条件:
| 层级 | 控制内容 |
| -------------- | ---------- |
| OAuth | 用户身份 |
| API Enablement | 是否允许调用 API |
👉 即使 OAuth 成功,如果 API 未启用:
也会报错:
403 accessNotConfigured
### 2. 典型错误示例
Gmail API has not been used in project XXX
👉 表示:
该 Project 未启用 Gmail API
### 3. 操作步骤
#### Step 1进入 API 页面
Google Cloud Console
→ APIs & Services
→ Library
#### Step 2搜索 API
例如:
Gmail API
#### Step 3启用
Enable
#### Step 4等待生效
30 秒 ~ 2 分钟(有延迟)
#### Step 5重新授权关键
gog auth revoke
gog auth login
👉 原因:
旧 token 不包含新权限
---
## 验证配置
### 1. 查看已授权的账号
```bash
gog auth list
```
- 正确结果
```
weishen@WeideMac-mini ~ % gog auth list
ishenwei@gmail.com default calendar,contacts,docs,drive,gmail,sheets 2026-03-24T01:29:14Z oauth
```
### 2. 测试 Gmail
```bash
gog gmail search "newer_than:1d" --max 5
```
- 正确结果:
```
weishen@WeideMac-mini ~ % gog auth list
ishenwei@gmail.com default calendar,contacts,docs,drive,gmail,sheets 2026-03-24T01:29:14Z oauth
```
### 3. 测试 Calendar
```bash
gog calendar events primary --from 2026-01-01 --to 2026-12-31
```
---
## 常用命令
### Gmail
| 功能 | 命令 |
|------|------|
| 搜索邮件 | `gog gmail search 'newer_than:7d' --max 10` |
| 发送邮件 | `gog gmail send --to a@b.com --subject "Hi" --body "Hello"` |
| 发送邮件(多行) | `gog gmail send --to a@b.com --subject "Hi" --body-file ./message.txt` |
| 创建草稿 | `gog gmail drafts create --to a@b.com --subject "Hi" --body-file ./message.txt` |
| 发送草稿 | `gog gmail drafts send <draftId>` |
### Calendar
| 功能 | 命令 |
|------|------|
| 查看事件 | `gog calendar events <calendarId> --from <iso> --to <iso>` |
| 创建事件 | `gog calendar create <calendarId> --summary "Title" --from <iso> --to <iso>` |
| 查看颜色 | `gog calendar colors` |
### Drive
| 功能 | 命令 |
|------|------|
| 搜索文件 | `gog drive search "query" --max 10` |
### Contacts
| 功能 | 命令 |
|------|------|
| 列出联系人 | `gog contacts list --max 20` |
### Sheets
| 功能 | 命令 |
|------|------|
| 获取数据 | `gog sheets get <sheetId> "Tab!A1:D10" --json` |
| 更新数据 | `gog sheets update <sheetId> "Tab!A1:B2" --values-json '[["A","B"],["1","2"]]' --input USER_ENTERED` |
### Docs
| 功能 | 命令 |
|------|------|
| 导出文档 | `gog docs export <docId> --format txt --out /tmp/doc.txt` |
| 查看内容 | `gog docs cat <docId>` |
---
## 故障排除
### 凭证文件路径错误
确保凭证文件在以下位置:
```
/Users/weishen/Library/Application Support/gogcli/credentials.json
```
### 需要重新授权
删除现有授权并重新授权:
```bash
gog auth remove ishenwei@gmail.com
gog auth add ishenwei@gmail.com --services gmail,calendar,drive,contacts,docs,sheets
```
### 设置默认账号
避免每次重复指定账号:
```
cd ~
nano .zshrc
```
在.zshrc中添加以下设定
```bash
export GOG_ACCOUNT=ishenwei@gmail.com
```
保存生效
```
source ~/.zshrc
```
---
## 参考链接
- gog 官网: https://gogcli.sh
- gog GitHub: https://github.com/steipete/gogcli
- Google Cloud Console: https://console.cloud.google.com/
---
*文档创建日期: 2026-03-15*
*最后更新: 2026-03-15*