每个 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 provide a way for users to go back or cancel, if they choose not to link.
- 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.
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 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 会在用户的浏览器中打开您的授权端点。如果流程是在仅支持语音的设备上针对 Action 启动的,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 。
|
user_locale |
采用 RFC5646 格式的 Google 账号语言设置,用于将您的内容本地化为用户的首选语言。 |
例如,如果您的授权端点位于 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&user_locale=LOCALE
如需让授权端点处理登录请求,请执行以下步骤:
- 验证
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 集成:
在项目列表中,点击要处理的项目旁边的打开。
在云到云下,选择开发。
点击集成旁边的打开。
向下滚动到权限(可选)部分,然后选中允许 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 生成刷新令牌和访问令牌。这些令牌可以是任何字符串值,但必须能唯一代表相应用户和令牌所针对的客户端,并且不得可猜测。对于访问令牌,还应记录令牌的到期时间,该时间通常为您签发令牌后的 1 小时。刷新令牌不会过期。
- 在 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 生成访问令牌。这些令牌可以是任何字符串值,但必须能唯一代表令牌所代表的用户和客户端,并且不得可猜测。对于访问令牌,还应记录令牌的到期时间,通常为您签发令牌后的 1 小时。
- 在 HTTPS 响应的正文中返回以下 JSON 对象:
{
"token_type": "Bearer",
"access_token": "ACCESS_TOKEN ",
"expires_in":SECONDS_TO_EXPIRATION
}
Handle userinfo requests
The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:
- Linked Account Sign-In with Google One Tap.
- Frictionless subscription on AndroidTV.
After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.
userinfo endpoint request headers | |
---|---|
Authorization header |
The access token of type Bearer. |
For example, if your userinfo endpoint is available at
https://myservice.example.com/userinfo
, a request might look like the following:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
For your userinfo endpoint to handle requests, do the following steps:
- Extract access token from the Authorization header and return information for the user associated with the access token.
- If the access token is invalid, return an HTTP 401 Unauthorized error with using the
WWW-Authenticate
Response Header. Below is an example of a userinfo error response: If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:
If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.{ "sub": "
USER_UUID ", "email": "EMAIL_ADDRESS ", "given_name": "FIRST_NAME ", "family_name": "LAST_NAME ", "name": "FULL_NAME ", "picture": "PROFILE_PICTURE ", }userinfo endpoint response sub
A unique ID that identifies the user in your system. email
Email address of the user. given_name
Optional: First name of the user. family_name
Optional: Last name of the user. name
Optional: Full name of the user. picture
Optional: Profile picture of the user.