介绍
# Google Slides
通过受管理的 OAuth 认证访问 Google 幻灯片 API。创建和管理演示文稿,添加幻灯片,插入文本和图片,以及控制格式。
## 快速开始
```bash # Create a new presentation python <<'EOF' import urllib.request, os, json data = json.dumps({'title': 'My Presentation'}).encode() req = urllib.request.Request('https://gateway.maton.ai/google-slides/v1/presentations', 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 ```
## 基础 URL
``` https://gateway.maton.ai/google-slides/{native-api-path} ```
将 `{native-api-path}` 替换为实际的 Google 幻灯片 API 端点路径。网关将请求代理到 `slides.googleapis.com` 并自动注入您的 OAuth 令牌。
## 认证
所有请求都需要在 Authorization 头中包含 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-slides&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-slides'}).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-slides", "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 幻灯片连接,请使用 `Maton-Connection` 头指定要使用的连接:
```bash python <<'EOF' import urllib.request, os, json data = json.dumps({'title': 'My Presentation'}).encode() req = urllib.request.Request('https://gateway.maton.ai/google-slides/v1/presentations', data=data, method='POST') req.add_header('Authorization', f'Bearer {os.environ["MATON_API_KEY"]}') req.add_header('Content-Type', 'application/json') req.add_header('Maton-Connection', '21fd90f9-5935-43cd-b6c8-bde9d915ca80') print(json.dumps(json.load(urllib.request.urlopen(req)), indent=2)) EOF ```
如果省略,网关将使用默认(最早)的活动连接。
## API 参考
### 演示文稿 (Presentations)
#### 创建演示文稿
```bash POST /google-slides/v1/presentations Content-Type: application/json
{ "title": "My Presentation" } ```
#### 获取演示文稿
```bash GET /google-slides/v1/presentations/{presentationId} ```
### 页面 (幻灯片)
#### 获取页面
```bash GET /google-slides/v1/presentations/{presentationId}/pages/{pageId} ```
#### 获取页面缩略图
```bash GET /google-slides/v1/presentations/{presentationId}/pages/{pageId}/thumbnail ```
使用自定义尺寸:
```bash GET /google-slides/v1/presentations/{presentationId}/pages/{pageId}/thumbnail?thumbnailProperties.mimeType=PNG&thumbnailProperties.thumbnailSize=LARGE ```
### 批量更新
batchUpdate 端点用于大多数修改。它接受一组按原子方式应用的请求。
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [...] } ```
#### 创建幻灯片
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "createSlide": { "objectId": "slide_001", "slideLayoutReference": { "predefinedLayout": "TITLE_AND_BODY" } } } ] } ```
可用的预定义布局: - `BLANK` - `TITLE` - `TITLE_AND_BODY` - `TITLE_AND_TWO_COLUMNS` - `TITLE_ONLY` - `SECTION_HEADER` - `ONE_COLUMN_TEXT` - `MAIN_POINT` - `BIG_NUMBER`
#### 插入文本
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "insertText": { "objectId": "{shapeId}", "text": "Hello, World!", "insertionIndex": 0 } } ] } ```
#### 删除文本
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "deleteText": { "objectId": "{shapeId}", "textRange": { "type": "ALL" } } } ] } ```
#### 创建形状
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "createShape": { "objectId": "shape_001", "shapeType": "TEXT_BOX", "elementProperties": { "pageObjectId": "{slideId}", "size": { "width": {"magnitude": 300, "unit": "PT"}, "height": {"magnitude": 100, "unit": "PT"} }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 100, "translateY": 100, "unit": "PT" } } } } ] } ```
#### 创建图片
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "createImage": { "objectId": "image_001", "url": "https://example.com/image.png", "elementProperties": { "pageObjectId": "{slideId}", "size": { "width": {"magnitude": 200, "unit": "PT"}, "height": {"magnitude": 200, "unit": "PT"} }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 200, "translateY": 200, "unit": "PT" } } } } ] } ```
#### 删除对象
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "deleteObject": { "objectId": "{objectId}" } } ] } ```
#### 更新文本样式
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "updateTextStyle": { "objectId": "{shapeId}", "textRange": { "type": "ALL" }, "style": { "bold": true, "fontSize": {"magnitude": 24, "unit": "PT"}, "foregroundColor": { "opaqueColor": { "rgbColor": {"red": 0.2, "green": 0.4, "blue": 0.8} } } }, "fields": "bold,fontSize,foregroundColor" } } ] } ```
#### 替换所有文本
```bash POST /google-slides/v1/presentations/{presentationId}:batchUpdate Content-Type: application/json
{ "requests": [ { "replaceAllText": { "containsText": { "text": "{{placeholder}}", "matchCase": true }, "replaceText": "Actual Value" } } ] } ```
## 代码示例
### JavaScript
```javascript // Create a presentation const response = await fetch( 'https://gateway.maton.ai/google-slides/v1/presentations', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.MATON_API_KEY}` }, body: JSON.stringify({ title: 'My Presentation' }) } );
const presentation = await response.json(); const presentationId = presentation.presentationId;
// Add a slide await fetch( `https://gateway.maton.ai/google-slides/v1/presentations/${presentationId}:batchUpdate`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.MATON_API_KEY}` }, body: JSON.stringify({ requests: [ { createSlide: { slideLayoutReference: { predefinedLayout: 'TITLE_AND_BODY' } } } ] }) } ); ```
### Python
```python import os import requests
headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}' }
# Create a presentation response = requests.post( 'https://gateway.maton.ai/google-slides/v1/presentations', headers=headers, json={'title': 'My Presentation'} ) presentation = response.json() presentation_id = presentation['presentationId']
# Add a slide requests.post( f'https://gateway.maton.ai/google-slides/v1/presentations/{presentation_id}:batchUpdate', headers=headers, json={ 'requests': [ { 'createSlide': { 'slideLayoutReference': {'predefinedLayout': 'TITLE_AND_BODY'} } } ] } ) ```
## 注意事项
- 对象 ID 在演示文稿中必须是唯一的 - 对所有修改(添加幻灯片、文本、形状等)使用 batchUpdate - batchUpdate 中的多个请求将原子地应用 - 尺寸和位置使用 PT (points) 作为单位 (72 points = 1 inch) - 使用 `replaceAllText` 进行基于模板的演示文稿生成 - 重要:使用 curl 命令时,如果 URL 包含括号,请使用 `curl -g` 以禁用 glob 解析 - 重要:将 curl 输出通过管道传递给 `jq` 或其他命令时,`$MATON_API_KEY` 等环境变量在某些 shell 环境中可能无法正确展开。在通过管道传输时,您可能会遇到“Invalid API key”错误。
## 错误处理
| 状态 | 含义 | |--------|---------| | 400 | 缺少 Google 幻灯片连接 | | 401 | Maton API 密钥无效或缺失 | | 404 | 未找到演示文稿 | | 429 | 请求速率受限 (每个账户每秒 10 次请求) | | 4xx/5xx | 来自 Google 幻灯片 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-slides` 开头。例如:
- 正确:`https://gateway.maton.ai/google-slides/v1/presentations` - 错误:`https://gateway.maton.ai/slides/v1/presentations`
## 资源
- [幻灯片 API 概述](https://developers.google.com/slides/api/reference/rest) - [演示文稿](https://developers.google.com/slides/api/reference/rest/v1/presentations) - [页面](https://developers.google.com/slides/api/reference/rest/v1/presentations.pages) - [批量更新请求](https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate) - [页面布局](https://developers.google.com/slides/api/reference/rest/v1/presentations/create#predefinedlayout) - [Maton 社区](https://discord.com/invite/dBfFAcefs2) - [Maton 支持](mailto:[email protected])