Gmail API logo

Gmail API

Gmail API

正常免费analyticsGoogle

Gmail API 让应用代表 Google 账户读取、发送、组织邮件,是 Google Workspace 集成的核心入口。

访问站点 ↗健康巡检 9 小时前
适合什么时候用

配额慷慨——每用户每天 10 亿 quota units

先注意什么

必须走 OAuth 流程,不能用简单 API key 调用

第一步先查

到 console.cloud.google.com 创建项目,启用 Gmail API,配置 OAuth consent screen 与 credentials。代码端用 OAuth 2.0 拿 access_token,调 https://gmail.googleapis.com/gmail/v1/users/me/... 各端点。需要范围如 https://www.googleapis.com/auth/gmail.readonly。

认证
oauth
CORS
不支持
HTTPS
注册
需要
延迟
42 ms
协议
REST
计费
free

可用率 · 30 天窗口

巡检: 1可用率: 100%平均延迟: 42ms
01

关于这个 API

Gmail API 是 Google Workspace 生态中最常用的 API 之一,提供完整的邮箱编程能力:读写邮件、管理标签、操作草稿、查询邮件搜索语法、监听新邮件事件。和直接对接 IMAP/SMTP 相比,Gmail API 是结构化的 JSON 接口,速率配额更高,且能监听增量变更而不需轮询。

认证只有一条路:OAuth 2.0。这意味着你需要在 Google Cloud Console 注册 OAuth client,让用户授权,拿 refresh_token 长期使用。Workspace 域管理员可以为整个组织授权 service account(domain-wide delegation),适合做企业内部工具。

邮件主体以 base64url 编码的 RFC 2822 MIME 存储——读取 attachments 或 HTML 内容需要解析 MIME 树。Gmail API 提供 history 接口做增量同步:第一次全量拉,之后用 history_id 只拉变更。push notifications 通过 Pub/Sub 实时通知,避免反复 long-poll。配额按 quota units 计算,普通操作 5 units,发送 100 units,每用户每天 10 亿 quota units,对绝大多数应用绰绰有余。

02

你可以做什么

  • 1搭建邮件客户端或邮件分类工具
  • 2将关键邮件同步到外部 CRM / 工单系统
  • 3按规则自动归档、贴标、转发邮件
  • 4为应用提供"用 Gmail 发送"的合规通道
  • 5生成邮件智能摘要与回复建议
03

优劣对比

优点

  • 配额慷慨——每用户每天 10 亿 quota units
  • push notifications 通过 Pub/Sub 实时通知新邮件
  • 与 Workspace 其他 API(Calendar、Drive)共享 OAuth scope 模型

注意事项

  • 必须走 OAuth 流程,不能用简单 API key 调用
  • 消息体是 base64url 编码 MIME,解析略繁琐
  • 发送速率有反垃圾上限(约 250 封/秒/账户)
04

示例请求

通用模板 — 实际 endpoint 请查阅文档替换 <endpoint>。
curl https://google.com/<endpoint> \
  -H "Authorization: Bearer $ACCESS_TOKEN"
05

快速开始

到 console.cloud.google.com 创建项目,启用 Gmail API,配置 OAuth consent screen 与 credentials。代码端用 OAuth 2.0 拿 access_token,调 https://gmail.googleapis.com/gmail/v1/users/me/... 各端点。需要范围如 https://www.googleapis.com/auth/gmail.readonly。

06

常见问题

Gmail API 收费吗?+

免费使用,配额按 quota units 计算。普通应用极难触达上限。

能不通过用户授权用我的服务账号发邮件吗?+

只能在 Google Workspace 内通过 domain-wide delegation 让管理员授权 service account 代表组织内用户操作。

如何监听新邮件?+

用 Watch API 订阅 Pub/Sub 主题;新邮件触发 push,再用 history.list 拉变更 ID。

OAuth consent screen 需要 Google 审核吗?+

使用敏感 scope(如 gmail.readonly)的应用上线前需要通过 Google verification 流程,时间可能 2-6 周。

07

技术细节

CORS: NoHTTPS: Yes注册: Yes开源: No
认证方式
oauth
计费
free
协议
REST
SDK
python, javascript, java, go, ruby
响应时间
42 ms
上次巡检
2026/5/12 07:37:31
08

接口端点

从 OpenAPI spec 自动解析。显示 12 / 79 个未弃用端点。

GET
/gmail/v1/users/{userId}/draftsusers
userId:path*includeSpamTrash:querymaxResults:querypageToken:queryq:query
POST
/gmail/v1/users/{userId}/draftsusers
userId:path*
DELETE
/gmail/v1/users/{userId}/drafts/{id}users
userId:path*id:path*
GET
/gmail/v1/users/{userId}/drafts/{id}users
userId:path*id:path*format:query
PUT
/gmail/v1/users/{userId}/drafts/{id}users
userId:path*id:path*
POST
/gmail/v1/users/{userId}/drafts/sendusers
userId:path*
GET
/gmail/v1/users/{userId}/historyusers
userId:path*historyTypes:querylabelId:querymaxResults:querypageToken:query+1 更多
GET
/gmail/v1/users/{userId}/labelsusers
userId:path*
POST
/gmail/v1/users/{userId}/labelsusers
userId:path*
DELETE
/gmail/v1/users/{userId}/labels/{id}users
userId:path*id:path*
GET
/gmail/v1/users/{userId}/labels/{id}users
userId:path*id:path*
PATCH
/gmail/v1/users/{userId}/labels/{id}users
userId:path*id:path*

另有 67 个端点未显示,详见 OpenAPI spec。

09

标签

10

更多来自 Google 的 API