跳到主要内容

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 Docs
    • Google Sheets
    • Google Slides

    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 同意屏幕,Google 将重定向至 Google Auth Platform 概览页面

  3. 概览标签页选择开始设置以配置 OAuth 同意屏幕

  4. 输入将显示在 OAuth 屏幕上的应用名称用户支持邮箱,选择下一步继续

  5. 对于受众群体,选择内部供组织 Google Workspace 内用户访问,或选择外部供任何 Google 账户用户访问。有关用户类型的详细信息,请参阅 Google 的用户类型文档。选择下一步继续

  6. 选择 Google 应使用的邮箱地址以通知项目变更,选择下一步继续

  7. 阅读并接受 Google 用户数据政策,选择继续后选择创建

  8. 在左侧菜单选择品牌化

  9. 授权网域部分选择添加网域

    • 若使用 n8n Cloud 服务,添加 n8n.cloud
    • 若为自托管,添加 n8n 实例域名
  10. 在页面底部选择保存

创建 Google OAuth 客户端凭据#

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

  1. 访问 Google Cloud Console,确保处于正确项目中
  2. API 和服务部分选择凭据
  3. 选择 + 创建凭据 > OAuth 客户端 ID
  4. 应用类型下拉菜单中选择 Web 应用
  5. Google 将自动生成名称,请更新为在控制台中可识别的名称
  6. 从 n8n 凭据复制 OAuth 重定向 URL,粘贴至 Google Console 的授权重定向 URI
  7. 选择创建

完成 n8n 凭据配置#

完成 Google 项目和凭据配置后,完善 n8n 凭据:

  1. 从 Google 的 OAuth 客户端已创建 模态窗口复制客户端 ID,填入 n8n 凭据
  2. 从同一模态窗口复制客户端密钥,填入 n8n 凭据
  3. 必须提供此凭据的作用域,详细信息请参阅作用域。多个作用域请以空格分隔列表形式输入,例如:

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

  1. 在 n8n 中,选择 Sign in with Google 以完成 Google 身份验证。
  2. 保存您的新凭据。

视频#

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

作用域#

Google 服务具有一个或多个可能的访问作用域。作用域限制了用户可以执行的操作。有关所有服务的作用域列表,请参阅 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 凭据模态窗口中重新连接应用。