通过 OAuth 进行 Google 账号关联(隐式流程 - 已归档)

如需支持 OAuth 2.0 隐式 流程,您的服务需要通过 HTTPS 提供授权端点。此端点负责进行身份验证并征得用户同意以获取数据访问权限。授权端点会向尚未登录的用户显示登录界面,并记录用户对所请求访问权限的同意情况。

当 Google 应用需要调用您服务的某个已获授权的 API 时,Google 会使用此端点征得用户同意,以便代表用户调用这些 API。

Google 帐号关联:OAuth 隐式流程

以下序列图详细介绍了用户、Google 和您服务的端点之间的互动。

用户 Google 应用 / 浏览器 您的授权 端点 1. 用户发起关联 2. 重定向到授权端点 (GET) client_id、redirect_uri、state、scope 3. 显示登录和同意屏幕 4. 用户进行身份验证并授予同意 5. 使用令牌重定向回 Google (GET) access_token、state 6. 存储用户令牌 7. 访问用户资源
图 1.Google 帐号关联的 OAuth 2.0 隐式流程中的事件序列。

角色和职责

下表定义了 Google 帐号关联 (GAL) OAuth 隐式流程中参与者的角色和职责。请注意,在 GAL 中,Google 充当 OAuth 客户端,而您的服务充当 身份/服务提供商

参与者 / 组件 GAL 角色 职责
Google 应用 / 服务器 OAuth 客户端 发起流程,使用浏览器重定向接收访问令牌, 并安全地存储该令牌以访问您服务的 API。
您的授权端点 授权服务器 对用户进行身份验证,征得用户同意,并直接向 Google 发放长期有效的 访问令牌。
Google 重定向 URI 回调端点 从您的授权服务接收用户重定向,并在网址 片段中包含 access_tokenstate 值。

Google 发起的典型 OAuth 2.0 隐式流程会按以下流程进行:

  1. Google 在用户的浏览器中打开您的授权端点。用户登录(如果尚未登录),并授予 Google 权限以使用您的 API 访问其数据(如果尚未授予权限)。
  2. 您的服务会创建访问令牌并将其返回给 Google。 为此,请将用户的浏览器重定向回 Google,并在请求中附上访问令牌。
  3. Google 会调用您服务的 API,并在每个请求中附上访问令牌。您的服务会验证访问令牌是否授予 Google 访问 API 的授权,然后完成 API 调用。

实现方案

请按照以下步骤实现隐式流程。

第 1 步:处理授权请求

当 Google 发起帐号关联时,它会将用户重定向到您的授权端点。如需了解详细的协议合同和参数要求,请参阅授权端点

如需处理该请求,请执行以下操作:

  1. 验证请求

    • 确认 client_id 与分配给 Google 的客户端 ID 一致。
    • 确认 redirect_uri 与预期的 Google 重定向 网址: none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
    • 验证 response_type 是否为 token
  2. 对用户进行身份验证

    • 检查用户是否已登录您的服务。
    • 如果用户未登录,请提示用户完成登录或注册流程。
  3. 生成访问令牌:

    • 创建与用户和客户端关联的唯一且难以猜测的访问令牌。
  4. 重定向回 Google

    • 将浏览器重定向到 redirect_uri 中提供的网址。
    • 网址片段 (哈希)中附加以下参数:
      • access_token:您生成的访问令牌。
      • token_type:必须为 bearer
      • state:从 Google 收到的未修改的状态值。
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:

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:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. 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:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    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.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    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.