Google: Service Account#
Using service accounts is more complex than OAuth2. Before you begin:
- Check if your node is compatible with Service Account.
- Make sure you need to use Service Account. For most use cases, OAuth2 is a better option.
- Read the Google documentation on Creating and managing service accounts.
Prerequisites#
- Create a Google Cloud account.
Set up Service Account#
There are four steps to connecting your n8n credential to a Google Service Account:
- Create a Google Cloud Console project.
- Enable APIs.
- Set up Google Cloud Service Account.
- Finish your n8n credential.
Create a Google Cloud Console project#
First, create a Google Cloud Console project. If you already have a project, jump to the next section:
- 使用您的 Google 凭据登录到您的 Google Cloud 控制台。
- 在顶部菜单中,选择顶部导航中的项目下拉菜单并选择新建项目,或直接转到新建项目页面。
- 输入项目名称并为您的项目选择位置。
- 选择创建。
-
检查顶部导航并确保项目下拉菜单已选择您的项目。如果没有,请选择您刚创建的项目。
Enable APIs#
With your project created, enable the APIs you'll need access to:
- 访问您的 Google Cloud 控制台 - 库。确保您在正确的项目中。
检查 Google Cloud 顶部导航中的项目下拉菜单 - 转到 API 和服务 > 库。
- 搜索并选择您要启用的 API。例如,对于 Gmail 节点,搜索并启用 Gmail API。
-
某些集成需要其他 API 或需要您请求访问权限:
- Google Perspective:请求 API 访问权限。
- Google Ads:获取开发者令牌。
需要 Google Drive API
以下集成除了需要自己的 API 外,还需要 Google Drive API:
- Google Docs
- Google Sheets
- Google Slides
Google Vertex AI API
除了 Vertex AI API,您还需要启用 Cloud Resource Manager API。
-
选择启用。
Set up Google Cloud Service Account#
-
Access your Google Cloud Console - Library. Make sure you're in the correct project.
Check the project dropdown in the Google Cloud top navigation -
Select the hamburger menu > APIs & Services > Credentials. Google takes you to your Credentials page.
- Select + CREATE CREDENTIALS > Service account.
- Enter a name in Service account name and an ID in Service account ID. Refer to Creating a service account for more information.
- Select CREATE AND CONTINUE.
- Based on your use-case, you may want to Select a role and Grant users access to this service account using the corresponding sections.
- Select DONE.
- Select your newly created service account under the Service Accounts section. Open the KEYS tab.
- Select ADD KEY > Create new key.
- In the modal that appears, select JSON, then select CREATE. Google saves the file to your computer.
Finish your n8n credential#
With the Google project and credentials fully configured, finish the n8n credential:
- Open the downloaded JSON file.
- Copy the
client_email
and enter it in your n8n credential as the Service Account Email. -
Copy the
private_key
. Don't include the surrounding"
marks. Enter this as the Private Key in your n8n credential.Older versions of n8n
If you're running an n8n version older than 0.156.0, replace all instances of
\n
in the JSON file with new lines. -
Optional: Choose if you want to Impersonate a User (turned on).
- To use this option, you must Enable domain-wide delegation for the service account as a Google Workspace super admin.
- Enter the Email of the user you want to impersonate.
- If you plan to use this credential with the HTTP Request node, turn on Set up for use in HTTP Request node.
- With this setting turned on, you'll need to add Scope(s) for the node. n8n prepopulates some scopes. Refer to OAuth 2.0 Scopes for Google APIs for more information.
- Save your credentials.
Video#
The following video demonstrates the steps described above.
Troubleshooting#
Service Account can't access Google Drive files#
A Service Account can't access Google Drive files and folders that weren't shared with its associated user email.
- Access your Google Cloud Console and copy your Service Account email.
- Access your Google Drive and go to the designated file or folder.
- Right-click on the file or folder and select Share.
- Paste your Service Account email into Add People and groups.
- Select Editor for read-write access or Viewer for read-only access.
Enable domain-wide delegation#
To impersonate a user with a service account, you must enable domain-wide delegation for the service account.
Not recommended
Google recommends you avoid using domain-wide delegation, as it allows impersonation of any user (including super admins) and can pose a security risk.
To delegate domain-wide authority to a service account, you must be a super administrator for the Google Workspace domain. Then:
- From your Google Workspace domain's Admin console, select the hamburger menu, then select Security > Access and data control > API Controls.
- In the Domain wide delegation pane, select Manage Domain Wide Delegation.
- Select Add new.
- In the Client ID field, enter the service account's Client ID. To get the Client ID:
- Open your Google Cloud Console project, then open the Service Accounts page.
- Copy the OAuth 2 Client ID and use this as the Client ID for the Domain Wide Delegation.
- In the OAuth scopes field, enter a list of comma-separate scopes to grant your application access. For example, if your application needs domain-wide full access to the Google Drive API and the Google Calendar API, enter:
https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar
. - Select Authorize.
It can take from 5 minutes up to 24 hours before you can impersonate all users in your Workspace.