ClawSkills logoClawSkills

Google Docs

具有托管 OAuth 的 Google Docs API 集成。创建文档、插入文本、应用格式和管理内容。当用户希望交互

介绍

# Google Docs

通过托管的 OAuth 身份验证访问 Google Docs API。创建文档、插入和设置文本格式,以及管理文档内容。

## 快速开始

```bash # Get document python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/google-docs/v1/documents/{documentId}') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

## 基础 URL

``` https://gateway.maton.ai/google-docs/{native-api-path} ```

将 `{native-api-path}` 替换为实际的 Google Docs API 端点路径。网关将请求代理到 `docs.googleapis.com` 并自动注入您的 OAuth 令牌。

## 身份验证

所有请求都需要在 Authorization header 中包含 Maton API 密钥:

``` Authorization: Bearer $MATON_API_KEY ```

**环境变量:** 将您的 API 密钥设置为 `MATON_API_KEY`:

```bash export MATON_API_KEY="YOUR_API_KEY" ```

### 获取 API 密钥

1. 登录或在 [maton.ai](https://maton.ai) 创建账户 2. 前往 [maton.ai/settings](https://maton.ai/settings) 3. 复制您的 API 密钥

## 连接管理

在 `https://ctrl.maton.ai` 管理您的 Google OAuth 连接。

### 列出连接

```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections?app=google-docs&status=ACTIVE') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

### 创建连接

```bash python <<'EOF' import urllib.request, os, json data = json.dumps({'app': 'google-docs'}).encode() req = urllib.request.Request('https://ctrl.maton.ai/connections', data=data, method='POST') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Content-Type', 'application/json') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

### 获取连接

```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

**响应:** ```json { "connection": { "connection_id": "21fd90f9-5935-43cd-b6c8-bde9d915ca80", "status": "ACTIVE", "creation_time": "2025-12-08T07:20:53.488460Z", "last_updated_time": "2026-01-31T20:03:32.593153Z", "url": "https://connect.maton.ai/?session_token=...", "app": "google-docs", "metadata": {} } } ```

在浏览器中打开返回的 `url` 以完成 OAuth 授权。

### 删除连接

```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections/{connection_id}', method='DELETE') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

### 指定连接

如果您有多个 Google Docs 连接,请使用 `Maton-Connection` header 指定要使用的连接:

```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/google-docs/v1/documents/{documentId}') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Maton-Connection', '21fd90f9-5935-43cd-b6c8-bde9d915ca80') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

如果省略,网关将使用默认(最旧的)活动连接。

## API 参考

### 获取文档

```bash GET /google-docs/v1/documents/{documentId} ```

### 创建文档

```bash POST /google-docs/v1/documents Content-Type: application/json

{ "title": "New Document" } ```

### 批量更新文档

```bash POST /google-docs/v1/documents/{documentId}:batchUpdate Content-Type: application/json

{ "requests": [ { "insertText": { "location": {"index": 1}, "text": "Hello, World!" } } ] } ```

## 常见 batchUpdate 请求

### 插入文本

```json { "insertText": { "location": {"index": 1}, "text": "Text to insert" } } ```

### 删除内容

```json { "deleteContentRange": { "range": { "startIndex": 1, "endIndex": 10 } } } ```

### 替换所有文本

```json { "replaceAllText": { "containsText": { "text": "{{placeholder}}", "matchCase": true }, "replaceText": "replacement value" } } ```

### 插入表格

```json { "insertTable": { "location": {"index": 1}, "rows": 3, "columns": 3 } } ```

### 更新文本样式

```json { "updateTextStyle": { "range": { "startIndex": 1, "endIndex": 10 }, "textStyle": { "bold": true, "fontSize": {"magnitude": 14, "unit": "PT"} }, "fields": "bold,fontSize" } } ```

### 插入分页符

```json { "insertPageBreak": { "location": {"index": 1} } } ```

## 代码示例

### JavaScript

```javascript // Create document const response = await fetch( 'https://gateway.maton.ai/google-docs/v1/documents', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.MATON_API_KEY}` }, body: JSON.stringify({ title: 'New Document' }) } );

// Insert text await fetch( `https://gateway.maton.ai/google-docs/v1/documents/${docId}:batchUpdate`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.MATON_API_KEY}` }, body: JSON.stringify({ requests: [{ insertText: { location: { index: 1 }, text: 'Hello!' } }] }) } ); ```

### Python

```python import os import requests

# Create document response = requests.post( 'https://gateway.maton.ai/google-docs/v1/documents', headers={'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'}, json={'title': 'New Document'} ) ```

## 注意事项

- 索引位置从 1 开始(文档从索引 1 开始) - 使用 `endOfSegmentLocation` 在末尾追加 - batchUpdate 中的多个请求是原子性应用的 - 先获取文档以找到更新的正确索引 - 样式更新中的 `fields` 参数使用字段掩码语法 - 重要:使用 curl 命令时,如果 URL 包含括号(`fields[]`、`sort[]`、`records[]`),请使用 `curl -g` 以禁用 glob 解析 - 重要:将 curl 输出通过管道传递给 `jq` 或其他命令时,`$MATON_API_KEY` 等环境变量在某些 shell 环境中可能无法正确展开。通过管道传输时,您可能会收到“Invalid API key”(无效的 API 密钥)错误。

## 错误处理

| Status | Meaning | |--------|---------| | 400 | 缺少 Google Docs 连接 | | 401 | Maton API 密钥无效或缺失 | | 429 | 速率受限(每账户每秒 10 次请求) | | 4xx/5xx | 来自 Google Docs API 的透传错误 |

### 故障排除:API 密钥问题

1. 检查是否已设置 `MATON_API_KEY` 环境变量:

```bash echo $MATON_API_KEY ```

2. 通过列出连接来验证 API 密钥是否有效:

```bash python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://ctrl.maton.ai/connections') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```

### 故障排除:无效的应用名称

1. 确保您的 URL 路径以 `google-docs` 开头。例如:

- 正确:`https://gateway.maton.ai/google-docs/v1/documents/{documentId}` - 错误:`https://gateway.maton.ai/docs/v1/documents/{documentId}`

## 资源

- [Docs API 概述](https://developers.google.com/docs/api/how-tos/overview) - [获取文档](https://developers.google.com/docs/api/reference/rest/v1/documents/get) - [创建文档](https://developers.google.com/docs/api/reference/rest/v1/documents/create) - [批量更新](https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate) - [请求类型](https://developers.google.com/docs/api/reference/rest/v1/documents/request) - [文档结构](https://developers.google.com/docs/api/concepts/structure) - [Maton 社区](https://discord.com/invite/dBfFAcefs2) - [Maton 支持](mailto:[email protected])

更多产品