介绍
# Google Calendar
使用托管的 OAuth 身份验证访问 Google 日历 API。创建和管理事件、列出日历以及检查忙闲状态。
## 快速开始
```bash # List upcoming events python <<'EOF' import urllib.request, os, json req = urllib.request.Request('https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events?maxResults=10&orderBy=startTime&singleEvents=true') 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-calendar/{native-api-path} ```
将 `{native-api-path}` 替换为实际的 Google 日历 API 端点路径。网关将请求代理到 `www.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-calendar&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-calendar'}).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-calendar", "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 req = urllib.request.Request('https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events') 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-calendar/calendar/v3/users/me/calendarList ```
### 获取日历
```bash GET /google-calendar/calendar/v3/calendars/{calendarId} ```
对用户的主日历使用 `primary`。
### 列出事件
```bash GET /google-calendar/calendar/v3/calendars/primary/events?maxResults=10&orderBy=startTime&singleEvents=true ```
带有时间边界:
```bash GET /google-calendar/calendar/v3/calendars/primary/events?timeMin=2024-01-01T00:00:00Z&timeMax=2024-12-31T23:59:59Z&singleEvents=true&orderBy=startTime ```
### 获取事件
```bash GET /google-calendar/calendar/v3/calendars/primary/events/{eventId} ```
### 创建事件
```bash POST /google-calendar/calendar/v3/calendars/primary/events Content-Type: application/json
{ "summary": "Team Meeting", "description": "Weekly sync", "start": { "dateTime": "2024-01-15T10:00:00", "timeZone": "America/Los_Angeles" }, "end": { "dateTime": "2024-01-15T11:00:00", "timeZone": "America/Los_Angeles" }, "attendees": [ {"email": "[email protected]"} ] } ```
### 创建全天事件
```bash POST /google-calendar/calendar/v3/calendars/primary/events Content-Type: application/json
{ "summary": "All Day Event", "start": {"date": "2024-01-15"}, "end": {"date": "2024-01-16"} } ```
### 更新事件
```bash PUT /google-calendar/calendar/v3/calendars/primary/events/{eventId} Content-Type: application/json
{ "summary": "Updated Meeting Title", "start": {"dateTime": "2024-01-15T10:00:00Z"}, "end": {"dateTime": "2024-01-15T11:00:00Z"} } ```
### 修补事件(部分更新)
```bash PATCH /google-calendar/calendar/v3/calendars/primary/events/{eventId} Content-Type: application/json
{ "summary": "New Title Only" } ```
### 删除事件
```bash DELETE /google-calendar/calendar/v3/calendars/primary/events/{eventId} ```
### 快速添加事件(自然语言)
```bash POST /google-calendar/calendar/v3/calendars/primary/events/quickAdd?text=Meeting+with+John+tomorrow+at+3pm ```
### 忙/闲查询
```bash POST /google-calendar/calendar/v3/freeBusy Content-Type: application/json
{ "timeMin": "2024-01-15T00:00:00Z", "timeMax": "2024-01-16T00:00:00Z", "items": [{"id": "primary"}] } ```
## 代码示例
### JavaScript
```javascript // List events const response = await fetch( 'https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events?maxResults=10&singleEvents=true', { headers: { 'Authorization': `Bearer ${process.env.MATON_API_KEY}` } } );
// Create event await fetch( 'https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.MATON_API_KEY}` }, body: JSON.stringify({ summary: 'Meeting', start: { dateTime: '2024-01-15T10:00:00Z' }, end: { dateTime: '2024-01-15T11:00:00Z' } }) } ); ```
### Python
```python import os import requests
headers = {'Authorization': f'Bearer {os.environ["MATON_API_KEY"]}'}
# List events events = requests.get( 'https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events', headers=headers, params={'maxResults': 10, 'singleEvents': 'true'} ).json()
# Create event response = requests.post( 'https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events', headers=headers, json={ 'summary': 'Meeting', 'start': {'dateTime': '2024-01-15T10:00:00Z'}, 'end': {'dateTime': '2024-01-15T11:00:00Z'} } ) ```
## 注意事项
- 对用户的主日历使用 `primary` 作为 calendarId - 时间必须采用 RFC3339 格式(例如 `2024-01-15T10:00:00Z`) - 对于重复事件,使用 `singleEvents=true` 展开实例 - `orderBy=startTime` 需要 `singleEvents=true` - 重要:使用 curl 命令时,如果 URL 包含方括号(`fields[]`、`sort[]`、`records[]`),请使用 `curl -g` 以禁用 glob 解析 - 重要:当将 curl 输出通过管道传递给 `jq` 或其他命令时,像 `$MATON_API_KEY` 这样的环境变量可能无法在某些 shell 环境中正确展开。通过管道传输时,您可能会收到“Invalid API key”错误。
## 错误处理
| 状态 | 含义 | |--------|---------| | 400 | 缺少 Google 日历连接 | | 401 | Maton API 密钥无效或缺失 | | 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-calendar` 开头。例如:
- 正确:`https://gateway.maton.ai/google-calendar/calendar/v3/calendars/primary/events` - 错误:`https://gateway.maton.ai/calendar/v3/calendars/primary/events`
## 资源
- [日历 API 概述](https://developers.google.com/calendar/api/v3/reference) - [列出日历](https://developers.google.com/workspace/calendar/api/v3/reference/calendarList/list) - [列出事件](https://developers.google.com/workspace/calendar/api/v3/reference/events/list) - [获取事件](https://developers.google.com/workspace/calendar/api/v3/reference/events/get) - [插入事件](https://developers.google.com/workspace/calendar/api/v3/reference/events/insert) - [更新事件](https://developers.google.com/workspace/calendar/api/v3/reference/events/update) - [删除事件](https://developers.google.com/workspace/calendar/api/v3/reference/events/delete) - [快速添加事件](https://developers.google.com/workspace/calendar/api/v3/reference/events/quickAdd) - [忙/闲查询](https://developers.google.com/workspace/calendar/api/v3/reference/freebusy/query) - [Maton 社区](https://discord.com/invite/dBfFAcefs2) - [Maton 支持](mailto:[email protected])