每个 Cloud-to-cloud 集成都必须包含用户身份验证机制。
通过身份验证,您可以将用户的 Google 账号与用户在您的身份验证系统中的账号相关联。这样,您就可以在您的执行方式接收到智能家居 intent 时识别用户。Google 智能家居仅支持包含授权代码流程的 OAuth。
本页介绍了如何设置 OAuth 2.0 服务器,以使其可与 Cloud-to-cloud 集成搭配使用。
通过 OAuth 进行 Google 账号关联
In the authorization code flow, you need two endpoints:
The authorization endpoint, which presents the sign-in UI to your users that aren't already signed in. The authorization endpoint also creates a short-lived authorization code to record users' consent to the requested access.
The token exchange endpoint, which is responsible for two types of exchanges:
- Exchanges an authorization code for a long-lived refresh token and a short-lived access token. This exchange happens when the user goes through the account linking flow.
- Exchanges a long-lived refresh token for a short-lived access token. This exchange happens when Google needs a new access token because the one it had expired.
Design guidelines
This section describes the design requirements and recommendations for the user screen that you host for OAuth linking flows. After it's called by Google's app, your platform displays a sign in to Google page and account linking consent screen to the user. The user is directed back to Google's app after giving their consent to link accounts.

Requirements
- You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.
- You must have a Google authorization statement such as "By signing in, you are authorizing Google to control your devices." See the Google Device Control Authorization section of the Google Home Developer Policies.
- You must open the Web OAuth linking page and ensure users have a clear method for signing in to their Google Account, such as fields for their username and password. Don't use the Google Sign-In (GSI) method that enables users to link without being taken to the Web OAuth Linking page. It is a violation of Google policy.
- You must include at least one of the following items in the OAuth linking
page to indicate the integration to which the user is linking:
- Company logo
- Company name
- Integration name
- App icon
Recommendations
We recommend that you do the following:
Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.
Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.
Clear call-to-action. State a clear call-to-action on your consent screen, such as “Agree and link.” This is because users need to understand what data they're required to share with Google to link their accounts.
Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.
Clear sign-in process. Ensure that users have a clear method for signing in to their Google Account, such as fields for their username and password or Sign in with Google.
Ability to unlink. Offer a mechanism for users to unlink, such as a URL to their account settings on your platform. Alternatively, you can include a link to Google Account where users can manage their linked account.
Ability to change user account. Suggest a method for users to switch their account(s). This is especially beneficial if users tend to have multiple accounts.
- If a user must close the consent screen to switch accounts, send a recoverable error to Google so the user can sign in to the desired account with OAuth linking.
Include your logo. Display your company logo on the consent screen. Use your style guidelines to place your logo. If you wish to also display Google's logo, see Logos and trademarks.

授权代码流程
授权代码流程的 OAuth 2.0 服务器实现包含两个端点,您的服务通过 HTTPS 提供这两个端点。第一个端点是授权端点,负责查找或获取用户对数据访问的同意情况。授权端点会向尚未登录的用户显示登录界面,并记录用户对所请求访问权限的同意情况。第二个端点是令牌交换端点,用于获取加密字符串(称为令牌),这些令牌可授权用户访问您的服务。
当 Google 应用需要调用您某项服务的 API 时,Google 会同时使用这些端点来征得用户同意,以便代表用户调用这些 API。
由 Google 发起的 OAuth 2.0 授权代码流程会话具有以下流程:
- Google 会在用户的浏览器中打开您的授权端点。如果流程在仅支持语音的设备上针对某项操作启动,Google 会将执行转移到手机。
- 用户登录(如果尚未登录),并授予 Google 权限以通过您的 API 访问其数据(如果尚未授予权限)。
- 您的服务会创建授权代码并将其返回给 Google。为此,请将用户的浏览器重定向回 Google,并在请求中附上授权代码。
- Google 会将授权代码发送到您的令牌交换端点,该端点会验证代码的真实性,并返回访问令牌和刷新令牌。访问令牌是一种短期有效的令牌,您的服务会将其作为凭据来接受,以便访问 API。刷新令牌是一种长期有效的令牌,Google 可以存储并使用它在访问令牌过期时获取新的访问令牌。
- 用户完成账号关联流程后,Google 发送的每个后续请求都包含一个访问令牌。
处理授权请求
当您需要使用 OAuth 2.0 授权码流程执行账号关联时,Google 会将用户发送到您的授权端点,并发送包含以下参数的请求:
授权端点参数 | |
---|---|
client_id |
您分配给 Google 的客户端 ID。 |
redirect_uri |
您将对此请求的响应发送到的网址。 |
state |
一种簿记值,在重定向 URI 中原封不动地传递回 Google。 |
scope |
可选:一组以空格分隔的范围字符串,用于指定 Google 请求授权的数据。 |
response_type |
要在响应中返回的值的类型。对于 OAuth 2.0 授权代码流程,响应类型始终为 code 。 |
例如,如果您的授权端点位于 https://myservice.example.com/auth
,则请求可能如下所示:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code
如需让授权端点处理登录请求,请执行以下步骤:
- 验证
client_id
是否与您分配给 Google 的客户端 ID 相匹配,以及redirect_uri
是否与 Google 为您的服务提供的重定向网址相匹配。这些检查对于防止向意外或配置错误的客户端应用授予访问权限非常重要。如果您支持多个 OAuth 2.0 流程,还请确认response_type
为code
。 - 检查用户是否已登录您的服务。如果用户未登录,请完成服务的登录或注册流程。
- 生成授权代码,供 Google 用于访问您的 API。 授权代码可以是任何字符串值,但必须唯一表示用户、令牌所针对的客户端和代码的过期时间,并且不得可猜测。您通常会颁发大约 10 分钟后过期的授权码。
- 确认
redirect_uri
参数指定的网址具有以下格式:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- 将用户的浏览器重定向到
redirect_uri
参数指定的网址。通过附加code
和state
参数进行重定向时,请添加您刚刚生成的授权代码和原始的未修改状态值。以下是生成的网址示例:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
处理令牌交换请求
您服务的令牌交换端点负责处理两种类型的令牌交换:
- 将授权代码换成访问令牌和刷新令牌
- 将刷新令牌换成访问令牌
令牌交换请求包含以下参数:
令牌交换端点参数 | |
---|---|
client_id |
此字符串用于将请求源标识为 Google。此字符串必须在您的系统中注册为 Google 的唯一标识符。 |
client_secret |
您向 Google 注册的用于服务的密钥字符串。 |
grant_type |
要交换的令牌的类型。值为 authorization_code 或 refresh_token 。 |
code |
如果值为 grant_type=authorization_code ,此参数是 Google 从您的登录或令牌交换端点收到的代码。 |
redirect_uri |
如果值为 grant_type=authorization_code ,则此参数是初始授权请求中使用的网址。 |
refresh_token |
如果值为 grant_type=refresh_token ,则此参数是 Google 从您的令牌交换端点收到的刷新令牌。 |
配置 Google 向您的服务器发送凭据的方式
根据其实现,授权服务器希望在请求正文或请求标头中收到客户端凭据。
默认情况下,Google 会在请求正文中发送凭据。如果授权服务器要求客户端凭据位于请求标头中,您必须相应地配置 Cloud-to-cloud 集成:
在项目列表中,点击要处理的项目旁边的打开。
在 Cloud-to-Cloud 下,选择开发。
点击相应集成旁边的打开。
向下滚动到权限(可选)部分,然后选中允许 Google 通过 HTTP 基本 Auth 标头来传输客户端 ID 和客户端密钥复选框。
点击保存以保存更改。
将授权代码换成访问令牌和刷新令牌
用户登录后,您的授权端点会向 Google 返回一个时效很短的授权代码,然后 Google 会向您的令牌交换端点发送请求,以将该授权代码换成访问令牌和刷新令牌。
对于这些请求,grant_type
的值为 authorization_code
,code
的值为您之前授予 Google 的授权代码的值。以下是使用授权代码交换访问令牌和刷新令牌的请求示例:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI
如需使用授权代码换取访问令牌和刷新令牌,令牌交换端点会通过执行以下步骤来响应 POST
请求:
- 验证
client_id
是否将请求来源标识为授权来源,以及client_secret
是否与预期值匹配。 - 验证授权代码是否有效且未过期,以及请求中指定的客户端 ID 是否与授权代码关联的客户端 ID 相符。
- 确认
redirect_uri
参数指定的网址与初始授权请求中使用的值完全相同。 - 如果您无法验证上述所有条件,请返回 HTTP 400 错误请求错误,并将
{"error": "invalid_grant"}
作为正文。 - 否则,使用授权代码中的用户 ID 生成刷新令牌和访问令牌。这些令牌可以是任何字符串值,但必须能唯一表示用户和令牌所针对的客户端,并且不得可猜测。对于访问令牌,还要记录令牌的过期时间,该时间通常是您发放令牌后的一小时。刷新令牌不会过期。
- 在 HTTPS 响应的正文中返回以下 JSON 对象:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Google 会存储用户的访问令牌和刷新令牌,并记录访问令牌的过期时间。当访问令牌过期时,Google 会使用刷新令牌从您的令牌交换端点获取新的访问令牌。
将刷新令牌换成访问令牌
当访问令牌过期时,Google 会向您的令牌交换端点发送请求,以将刷新令牌换成新的访问令牌。
对于这些请求,grant_type
的值为 refresh_token
,而 refresh_token
的值为您之前授予 Google 的刷新令牌的值。以下是将刷新令牌换成访问令牌的请求示例:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
如需将刷新令牌换成访问令牌,令牌交换端点会通过执行以下步骤来响应 POST
请求:
- 验证
client_id
是否将请求源标识为 Google,以及client_secret
是否与预期值一致。 - 验证刷新令牌是否有效,以及请求中指定的客户端 ID 是否与刷新令牌关联的客户端 ID 相符。
- 如果您无法验证上述所有条件,请返回 HTTP 400 错误请求错误,并将
{"error": "invalid_grant"}
作为正文。 - 否则,请使用刷新令牌中的用户 ID 生成访问令牌。这些令牌可以是任意字符串值,但必须唯一表示用户和令牌所针对的客户端,并且不得可猜测。对于访问令牌,还要记录令牌的过期时间,通常是在您发放令牌后一小时。
- 在 HTTPS 响应的正文中返回以下 JSON 对象:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
处理 userinfo 请求
userinfo 端点是受 OAuth 2.0 保护的资源,会返回关联用户的声明。实现和托管 userinfo 端点是可选的,但以下用例除外:
从您的令牌端点成功检索到访问令牌后,Google 会向您的 userinfo 端点发送请求,以检索关联用户的基本个人资料信息。
userinfo 端点请求标头 | |
---|---|
Authorization header |
Bearer 类型的访问令牌。 |
例如,如果您的 userinfo 端点可通过
https://myservice.example.com/userinfo
时,请求可能如下所示:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
为了让 userinfo 端点能够处理请求,请执行以下步骤:
- 从 Authorization 标头中提取访问令牌,并返回与访问令牌相关联的用户的信息。
- 如果访问令牌无效,则使用
WWW-Authenticate
响应标头返回 HTTP 401 Unauthorized 错误。下面是一个 userinfo 错误响应示例: 如果在关联过程中返回 401 未经授权错误或任何其他失败的错误响应,该错误将无法恢复,检索到的令牌将被舍弃,并且用户必须重新开始关联流程。HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
如果访问令牌有效,则返回 HTTPS 正文中包含以下 JSON 对象的 HTTP 200 响应 回答:
如果您的 userinfo 端点返回 HTTP 200 成功响应,则系统会针对用户的 Google 账号注册检索到的令牌和声明。{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }
userinfo 端点响应 sub
系统中用于识别用户的唯一 ID。 email
用户的电子邮件地址。 given_name
可选:用户的名字。 family_name
可选:用户的姓氏。 name
可选:用户的全名。 picture
可选:用户的个人资料照片。