Skip to content

Google: 通用 OAuth2 认证#

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

给 n8n Cloud 用户的注意事项

对于以下节点,您可以通过在 OAuth 部分选择 使用 Google 登录 进行认证:

先决条件#

设置 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. 在顶部菜单中,选择顶部导航栏的项目下拉菜单,然后选择新建项目,或直接访问新建项目页面。
  3. 输入项目名称并选择项目的位置
  4. 点击创建
  5. 检查顶部导航栏,确保项目下拉菜单中已选中您的项目。如果没有,请选择您刚刚创建的项目。

    Google Cloud 顶部导航栏中的项目下拉菜单
    检查 Google Cloud 顶部导航栏中的项目下拉菜单

启用 API#

项目创建完成后,启用您需要访问的 API:

  1. 访问您的 Google Cloud Console - 库。确保您位于正确的项目中。
    Google Cloud 顶部导航栏中的项目下拉菜单
    检查 Google Cloud 顶部导航栏中的项目下拉菜单
  2. 进入 API 和服务 > 库
  3. 搜索并选择您要启用的 API。例如,对于 Gmail 节点,搜索并启用 Gmail API。
  4. 某些集成需要其他 API 或需要您申请访问权限:

    需要 Google Drive API

    以下集成除了需要自己的 API 外,还需要 Google Drive API:

    • Google 文档
    • Google 表格
    • Google 幻灯片

    Google Vertex AI API

    除了 Vertex AI API 外,您还需要启用 Cloud Resource Manager API

  5. 选择 启用

配置您的 OAuth 同意屏幕#

如果您尚未在 Google Cloud 项目中使用过 OAuth,您需要先配置 OAuth 同意屏幕:

  1. 访问您的 Google Cloud Console - 库。确保您选择了正确的项目。
    Google Cloud 顶部导航栏中的项目下拉菜单
    检查 Google Cloud 顶部导航栏中的项目下拉菜单
  2. 打开左侧导航菜单,前往 API 和服务 > OAuth 同意屏幕
  3. 选择 开始配置 以启动 OAuth 同意设置。
  4. 输入将在 OAuth 屏幕上显示的 应用名称用户支持邮箱
  5. 对于 受众群体,选择 内部 表示仅限您组织的 Google Workspace 用户访问,或选择 外部 表示允许任何拥有 Google 账号的用户访问。有关用户类型的更多信息,请参阅 Google 的用户类型文档
  6. 选择 Google 应使用的 电子邮件地址 来通知您项目变更。
  7. 阅读并接受 Google 的用户数据政策,然后选择 创建
  8. 在左侧菜单中选择 品牌
  9. 授权域名 部分,选择 添加域名
    • 如果您使用 n8n 云服务,请添加 n8n.cloud
    • 如果您是自托管,请添加您的 n8n 实例域名。
  10. 在页面底部选择 保存

创建 Google OAuth 客户端凭据#

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

  1. APIs & Services 部分,选择 Credentials
  2. 选择 + Create credentials > OAuth client ID
  3. Application type 下拉菜单中,选择 Web application
  4. Google 会自动生成一个 Name。将其更新为您在控制台中能识别的名称。
  5. 从您的 n8n 凭据中复制 OAuth Redirect URL,然后粘贴到 Google 控制台的 Authorized redirect URIs 中。
  6. 选择 Create

完成 n8n 凭据设置#

当 Google 项目和凭据完全配置好后,完成 n8n 凭据设置:

  1. 从 Google 的 OAuth client created 弹窗中复制 Client ID,输入到您的 n8n 凭据中。
  2. 从同一个 Google 弹窗中复制 Client Secret,输入到您的 n8n 凭据中。
  3. 您必须为此凭据提供作用域。更多信息请参考作用域。输入多个作用域时用空格分隔,例如:
    1
    https://www.googleapis.com/auth/gmail.labels https://www.googleapis.com/auth/gmail.addons.current.action.compose
    
  4. 在 n8n 中,选择 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 Contacts
  • https://www.googleapis.com/auth/contacts
Google Docs
  • https://www.googleapis.com/auth/documents
  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.file
Google Drive
  • 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
Realtime Database
  • 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 Sheets
  • https://www.googleapis.com/auth/drive.file
  • https://www.googleapis.com/auth/spreadsheets
Google Slide
  • https://www.googleapis.com/auth/drive.file
  • https://www.googleapis.com/auth/presentations
Google Tasks
  • https://www.googleapis.com/auth/tasks
Google Translate
  • https://www.googleapis.com/auth/cloud-translation
GSuite Admin
  • 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 尚未验证此应用。为避免此问题,您可以从要认证的同一账户创建 OAuth 凭据。

如需使用其他账户(由开发者或第三方)生成的凭据,请按照 Google Cloud 文档 | 授权错误:Google 尚未验证此应用 中的说明操作。

Google Cloud 应用变为未授权状态#

对于 发布状态 设为 测试中用户类型 设为 外部 的 Google Cloud 应用,授权同意和令牌将在七天后过期。更多信息请参阅 Google Cloud Platform 控制台帮助 | 设置 OAuth 同意屏幕。要解决此问题,请在 n8n 凭据模态窗口中重新连接应用。

注:保持原文档中的引用片段路径不变,这些片段内容将在实际构建时被插入。