Google:服务账号(Google: Service Account)#
使用服务账户比使用 OAuth2 更复杂。在开始之前:
🌐 Using service accounts is more complex than OAuth2. Before you begin:
先决条件(Prerequisites)#
- 创建一个 Google Cloud 账户。
设置服务帐号(Set up Service Account)#
将 n8n 凭据连接到 Google 服务帐号共有四个步骤:
🌐 There are four steps to connecting your n8n credential to a Google Service Account:
创建 Google Cloud Console 项目(Create a Google Cloud Console project)#
首先,创建一个 Google Cloud 控制台项目。如果你已经有项目,请跳到下一部分:
🌐 First, create a Google Cloud Console project. If you already have a project, jump to the next section:
- Log in to your Google Cloud Console using your Google credentials.
- In the top menu, select the project dropdown in the top navigation and select New project or go directly to the New Project page.
- Enter a Project name and select the Location for your project.
- Select Create.
-
Check the top navigation and make sure the project dropdown has your project selected. If not, select the project you just created.
Check the project dropdown in the Google Cloud top navigation
启用 API(Enable APIs)#
创建项目后,启用你需要访问的 API:
🌐 With your project created, enable the APIs you'll need access to:
- Access your Google Cloud Console - Library. Make sure you're in the correct project.
Check the project dropdown in the Google Cloud top navigation - Go to APIs & Services > Library.
- Search for and select the API(s) you want to enable. For example, for the Gmail node, search for and enable the Gmail API.
-
Some integrations require other APIs or require you to request access:
- Google Perspective: Request API Access.
- Google Ads: Get a Developer Token.
Google Drive API required
The following integrations require the Google Drive API, as well as their own API:
- Google Docs
- Google Sheets
- Google Slides
Google Vertex AI API
In addition to the Vertex AI API you will also need to enable the Cloud Resource Manager API.
-
Select ENABLE.
设置 Google Cloud 服务帐号(Set up Google Cloud Service Account)#
-
访问你的 Google Cloud 控制台 - 库。确保你在正确的项目中。
查看 Google Cloud 顶部导航中的项目下拉菜单 -
打开左侧导航菜单,转到 API 和服务 > 凭据。谷歌会将你带到 凭据 页面。
- 选择 + 创建凭据 > 服务账号。
- 在 服务账号名称 中输入名称,在 服务账号 ID 中输入 ID。更多信息请参阅 创建服务账号。
- 选择 创建并继续。
- 根据你的使用情况,你可能需要使用相应的部分 选择角色 并 授予用户访问此服务账户的权限。
- 选择 完成。
- 在 服务账号 部分选择你新创建的服务账号。打开 密钥 选项卡。
- 选择 添加密钥 > 创建新密钥。
- 在出现的模态窗口中,选择 JSON,然后选择 创建。Google 会将文件保存到你的计算机上。
完成 n8n 凭据(Finish your n8n credential)#
完成 Google 项目和凭据配置后,完成 n8n 凭据配置:
🌐 With the Google project and credentials fully configured, finish the n8n credential:
- 打开下载的 JSON 文件。
- 复制
client_email并在你的 n8n 凭证中将其输入为 服务账户电子邮件。 -
复制
private_key。不要包含周围的"标记。在你的 n8n 凭证中将其作为 私钥 输入。n8n的旧版本
如果你使用的 n8n 版本低于 0.156.0,请将 JSON 文件中所有的
\n替换为新行。 -
可选:选择是否要模拟用户(已开启)。
- 要使用此选项,你必须以 Google Workspace 超级管理员身份为服务账户启用整个域委派。
- 输入你想要冒充的用户的电子邮件。
- 如果你计划在 HTTP 请求 节点中使用此凭据,请开启 在 HTTP 请求节点中设置使用。
- 开启此设置后,你需要为节点添加作用域。n8n 会预先填充一些作用域。有关更多信息,请参阅 Google API 的 OAuth 2.0 作用域。
- 保存你的凭据。
视频(Video)#
故障排除(Troubleshooting)#
服务帐号无法访问 Google 云端硬盘文件(Service Account can't access Google Drive files)#
No access to my drive
Google no longer allows Service Accounts created after April 15, 2025 to access my drive. Service Accounts now only have access to shared drives.
While not recommended, if you need to use a Service Account to access my drive, you can do so by enabling domain-wide delegation. You can learn more in this post in the community.
服务帐号无法访问未与其关联用户电子邮件共享的 Google 云端硬盘文件和文件夹。
🌐 A Service Account can't access Google Drive files and folders that weren't shared with its associated user email.
- 访问你的 Google Cloud 控制台 并复制你的服务账号邮箱。
- 访问你的 Google 云端硬盘 并转到指定的文件或文件夹。
- 右键点击文件或文件夹,然后选择 共享。
- 将你的服务账户电子邮件粘贴到 添加人员和组 中。
- 选择 编辑者 以获得可读写权限,或选择 查看者 以获得只读权限。
启用域范围委托(Enable domain-wide delegation)#
要使用服务账户模拟用户,你必须为该服务账户启用域范围委派。
🌐 To impersonate a user with a service account, you must enable domain-wide delegation for the service account.
不推荐
Google 建议你避免使用域范围委托,因为它允许冒充任何用户(包括超级管理员),可能带来安全风险。
要将整个域的权限授权给服务帐号,你必须是 Google Workspace 域的超级管理员。然后:
🌐 To delegate domain-wide authority to a service account, you must be a super administrator for the Google Workspace domain. Then:
- 在你的 Google Workspace 域的管理员控制台中,选择汉堡菜单,然后选择 安全性 > 访问和数据控制 > API 控制。
- 在 域范围委派 窗格中,选择 管理域范围委派。
- 选择 新增。
- 在 客户端ID 字段中,输入服务账户的 客户端ID。获取客户端ID的方法如下:
- 打开你的 Google Cloud 控制台项目,然后打开 服务账号 页面。
- 复制 OAuth 2 客户端 ID 并将其用作 域范围委派 的 客户端 ID。
- 在 OAuth 范围 字段中,输入以逗号分隔的范围列表,以授予你的应用访问权限。例如,如果你的应用需要对 Google Drive API 和 Google Calendar API 的全域访问权限,请输入:
https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar。 - 选择 授权。
模拟工作区中的所有用户可能需要 5 分钟到 24 小时不等。
🌐 It can take from 5 minutes up to 24 hours before you can impersonate all users in your Workspace.