N8N中文教程
集成节点/Built in_nodes/Credentials/Google

Google: OAuth2 通用#

本文档包含创建用于 自定义操作 的通用 OAuth2 Google 凭据的说明。

n8n Cloud 用户注意 对于以下节点,您可以在 OAuth 部分选择 使用 Google 登录 进行身份验证:

  • Google Calendar
  • Google Contacts
  • Google Drive
  • Google Mail
  • Google Sheets
  • Google Sheets Trigger
  • Google Tasks

前提条件#

  • 创建一个 Google Cloud 账户。

配置 OAuth#

将您的 n8n 凭据连接到 Google 服务共有五个步骤:

  1. 创建 Google Cloud Console 项目。
  2. 启用 API。
  3. 配置 OAuth 同意屏幕。
  4. 创建 Google OAuth 客户端凭据。
  5. 完成您的 n8n 凭据。

创建 Google Cloud Console 项目#

首先,创建一个 Google Cloud Console 项目。如果你已经有一个项目,请跳转到下一节:

  1. 使用你的 Google 账号登录 Google Cloud Console。
  2. 在顶部菜单中,点击顶部导航栏的项目下拉菜单,选择 新建项目(New project),或直接访问 新建项目页面。
  3. 输入 项目名称(Project name) 并为你的项目选择 位置(Location)
  4. 点击 创建(Create)
  5. 检查顶部导航栏,确保项目下拉菜单中已选中你刚创建的项目。如果没有,请手动选择你刚刚创建的项目。 Google Cloud 顶部导航中的项目下拉菜单 检查 Google Cloud 顶部导航中的项目下拉菜单

启用 API#

创建项目后,你需要启用所需访问的 API:

  1. 访问你的 Google Cloud Console - API 库(Library),并确认你处于正确的项目中。Google Cloud 顶部导航中的项目下拉菜单 检查 Google Cloud 顶部导航中的项目下拉菜单
  2. 进入 API 和服务 > 库(APIs & Services > Library)
  3. 搜索并选择你需要启用的 API。例如,对于 Gmail 节点,请搜索并启用 Gmail API。
  4. 某些集成需要额外的 API 或需申请访问权限: _ Google Perspective:申请 API 访问权限 _ Google Ads:获取 开发者令牌(Developer Token) 必需的 Google Drive API 以下集成除了自身 API 外,还需要启用 Google Drive API: _ Google Docs _ Google Sheets * Google Slides Google Vertex AI API 除了 Vertex AI API 外,你还必须启用 Cloud Resource Manager API。
  5. 点击 启用(ENABLE)

配置你的 OAuth 同意屏幕#

如果你之前从未在 Google Cloud 项目中使用过 OAuth,则需要配置 OAuth 同意屏幕:

  1. 访问你的 Google Cloud Console - 库。确保你处于正确的项目中。Google Cloud 顶部导航中的项目下拉菜单 检查 Google Cloud 顶部导航栏中的项目下拉菜单
  2. 打开左侧导航菜单,进入 APIs & Services > OAuth consent screen(API 和服务 > OAuth 同意屏幕)。Google 将重定向你到 Google 认证平台的概览页面。
  3. Overview(概览)标签页中选择 Get started(开始),以启动 OAuth 同意配置。
  4. 输入 App name(应用名称)和 User support email(用户支持邮箱),这些信息将显示在 OAuth 屏幕上。选择 Next(下一步)继续。
  5. 对于 Audience(受众),如果你希望组织内的 Google Workspace 用户访问,请选择 Internal(内部);如果希望任何拥有 Google 账号的用户都能访问,请选择 External(外部)。更多关于用户类型的信息,请参考 Google 的 User type documentation(用户类型文档)。选择 Next 继续。
  6. 选择 Google 应该用于就项目变更联系你的 Email addresses(电子邮件地址)。选择 Next 继续。
  7. 阅读并接受 Google 的用户数据政策(User Data Policy)。选择 Continue(继续),然后选择 Create(创建)。
  8. 在左侧菜单中,选择 Branding(品牌设置)。
  9. Authorized domains(已授权域名)部分,选择 Add domain(添加域名):
    • 如果你正在使用 n8n 的云服务,请添加 n8n.cloud
    • 如果你是自托管,请添加你的 n8n 实例的域名。
  10. 在页面底部选择 Save(保存)。

创建你的 Google OAuth 客户端凭据#

接下来,在 Google 中创建 OAuth 客户端凭据:

  1. 访问你的 Google Cloud Console。确保你处于正确的项目中。
  2. APIs & Services(API 和服务)部分,选择 Credentials(凭据)。
  3. 选择 + Create credentials(创建凭据)> OAuth client ID(OAuth 客户端 ID)。
  4. Application type(应用类型)下拉菜单中,选择 Web application(Web 应用程序)。
  5. Google 会自动生成一个 Name(名称)。你可以将其更新为你在控制台中容易识别的名称。
  6. 从你的 n8n 凭据中复制 OAuth Redirect URL(OAuth 重定向 URL),并粘贴到 Google Console 的 Authorized redirect URIs(已授权重定向 URI)字段中。
  7. 选择 Create(创建)。

完成你的 n8n 凭据#

在完成 Google 项目和凭据的配置后,请继续完成 n8n 凭据的设置:

  1. 在 Google 的 OAuth 客户端已创建 弹窗中,复制 客户端 ID(Client ID),并将其填入你的 n8n 凭据中。
  2. 在同一个弹窗中,复制 客户端密钥(Client Secret),并将其填入你的 n8n 凭据中。
  3. 你必须为此凭据提供作用域(scopes)。更多信息请参考 作用域(Scopes)。多个作用域需以空格分隔的形式输入,例如:
1

| ``` https://www.googleapis.com/auth/gmail.labels https://www.googleapis.com/auth/gmail.addons.current.action.compose


---|---
4. 在 n8n 中,点击 **使用 Google 登录(Sign in with Google)** 以完成 Google 身份验证。
5. **保存** 你的新凭据。

## 视频演示#

以下视频演示了上述步骤:

## 作用域(Scopes)#

Google 服务包含一个或多个可能的访问作用域(scope),每个作用域限制了用户可执行的操作。有关所有服务的作用域列表,请参阅 Google API 的 OAuth 2.0 作用域。

n8n 并不支持所有作用域。当你创建通用的 Google OAuth2 API 凭据时,只能使用下方 **支持的作用域** 列表中的内容。如果你输入了一个 n8n 尚未支持的作用域,将无法生效。

支持的作用域

服务 | 可用作用域
---|---
Gmail |
  * `https://www.googleapis.com/auth/gmail.labels`
  * `https://www.googleapis.com/auth/gmail.addons.current.action.compose`
  * `https://www.googleapis.com/auth/gmail.addons.current.message.action`
  * `https://mail.google.com/`
  * `https://www.googleapis.com/auth/gmail.modify`
  * `https://www.googleapis.com/auth/gmail.compose`


Google Ads |
  * `https://www.googleapis.com/auth/adwords`


Google Analytics |
  * `https://www.googleapis.com/auth/analytics`
  * `https://www.googleapis.com/auth/analytics.readonly`


Google BigQuery |
  * `https://www.googleapis.com/auth/bigquery`


Google Books |
  * `https://www.googleapis.com/auth/books`


Google Calendar |
  * `https://www.googleapis.com/auth/calendar`
  * `https://www.googleapis.com/auth/calendar.events`


Google Cloud
Natural Language |
  * `https://www.googleapis.com/auth/cloud-language`
  * `https://www.googleapis.com/auth/cloud-platform`


Google Cloud
Storage |
  * `https://www.googleapis.com/auth/cloud-platform`
  * `https://www.googleapis.com/auth/cloud-platform.read-only`
  * `https://www.googleapis.com/auth/devstorage.full_control`
  * `https://www.googleapis.com/auth/devstorage.read_only`
  * `https://www.googleapis.com/auth/devstorage.read_write`

Google 联系人 |
  * `https://www.googleapis.com/auth/contacts`


Google 文档 |
  * `https://www.googleapis.com/auth/documents`
  * `https://www.googleapis.com/auth/drive`
  * `https://www.googleapis.com/auth/drive.file`


Google 云端硬盘 |
  * `https://www.googleapis.com/auth/drive`
  * `https://www.googleapis.com/auth/drive.appdata`
  * `https://www.googleapis.com/auth/drive.photos.readonly`


Google Firebase
Cloud Firestore |
  * `https://www.googleapis.com/auth/datastore`
  * `https://www.googleapis.com/auth/firebase`


Google Firebase
实时数据库 |
  * `https://www.googleapis.com/auth/userinfo.email`
  * `https://www.googleapis.com/auth/firebase.database`
  * `https://www.googleapis.com/auth/firebase`


Google Perspective |
  * `https://www.googleapis.com/auth/userinfo.email`


Google 表格 |
  * `https://www.googleapis.com/auth/drive.file`
  * `https://www.googleapis.com/auth/spreadsheets`


Google 幻灯片 |
  * `https://www.googleapis.com/auth/drive.file`
  * `https://www.googleapis.com/auth/presentations`


Google 任务 |
  * `https://www.googleapis.com/auth/tasks`


Google 翻译 |
  * `https://www.googleapis.com/auth/cloud-translation`


GSuite 管理员 |
  * `https://www.googleapis.com/auth/admin.directory.group`
  * `https://www.googleapis.com/auth/admin.directory.user`
  * `https://www.googleapis.com/auth/admin.directory.domain.readonly`
  * `https://www.googleapis.com/auth/admin.directory.userschema.readonly`


## 故障排除#
### Google 尚未验证此应用#
如果使用 OAuth 认证方式,你可能会看到警告信息 **Google 尚未验证此应用**。为避免此问题:
  * 如果你的应用 **用户类型(User Type)** 为 **内部(Internal)**,请使用你要认证的同一个账户创建 OAuth 凭据。
  * 如果你的应用 **用户类型(User Type)** 为 **外部(External)**,你可以将你的邮箱添加到应用的测试用户列表中:前往 **受众(Audience)**  页面,并将你用于登录的邮箱添加到 **测试用户(Test users)** 列表中。

如果你需要使用由其他账户(例如开发者或第三方)生成的凭据,请参阅 Google Cloud 文档 | 授权错误:Google 尚未验证此应用 中的说明。
### Google Cloud 应用变为未授权状态#
对于 **发布状态** 设置为 **测试中** 且 **用户类型** 设置为 **外部** 的 Google Cloud 应用,其授权同意和令牌将在七天后过期。更多信息请参考 Google Cloud Platform 控制台帮助 | 配置 OAuth 同意屏幕。要解决此问题,请在 n8n 的凭据弹窗中重新连接该应用。