Account linking with OAuth

  • OAuth linking supports the implicit and authorization code flows for account linking.

  • The implicit flow is simpler to implement, returning a long-lived access token directly to Google after successful sign-in.

  • The authorization code flow requires separate authorization and token exchange endpoints for managing access and refresh tokens, and is recommended if token expiration is needed.

  • To implement OAuth linking, configure your project in the Actions Console and implement an OAuth server with an authorization endpoint to handle authentication requests.

  • Design a voice user interface for the authentication flow by creating and customizing an account linking scene in Actions Builder.

The OAuth linking type supports two industry standard OAuth 2.0 flows, the implicit and authorization code flows.

在隐式代码流程中,Google 会在用户浏览器中打开您的授权端点。成功登录后,系统会向 Google 返回长期访问令牌。现在,从 Google 助理向你的 Action 发送的每个请求中都包含此访问令牌。

在授权代码流程中,您需要两个端点:

  • 授权端点,该端点负责向尚未登录的用户显示登录界面,并以短期授权代码的形式记录所请求的访问。
  • 令牌交换端点,负责两种类型的交换:
    1. 将授权代码交换为长期刷新令牌和短期访问令牌。用户完成帐号关联流程后,系统会进行这种交换。
    2. 将长期刷新令牌换成短期访问令牌。Google 需要新访问令牌时,由于此令牌已过期,因此会进行此交换。

虽然隐式代码流程的实现更简单,但 Google 建议通过隐式流程发出的访问令牌永远不会过期,因为将令牌过期与隐式流程一起使用会强制用户再次关联其帐号。如果出于安全考虑需要令牌到期,强烈建议您考虑使用身份验证代码流程。

Implement OAuth account linking

Configure the project

To configure your project to use OAuth linking, follow these steps:

  1. Open the Actions Console and select the project you want to use.
  2. Click the Develop tab and choose Account linking.
  3. Enable the switch next to Account linking.
  4. In the Account creation section, select No, I only want to allow account creation on my website.
  5. In Linking type, select OAuth and Implicit.

  6. In Client Information:

    • Assign a value to Client ID issued by your Actions to Google to identify requests coming from Google.
    • Insert the URLs for your Authorization and Token Exchange endpoints.
  1. Click Save.

Implement your OAuth server

为了支持 OAuth 2.0 隐式流程,您的服务会进行授权 端点。此端点负责 就数据访问征得用户同意。授权端点 向尚未登录的用户显示登录界面,并记录 同意所请求的访问。

当您的 Action 需要调用您的某项授权的 API 时,Google 会使用 此端点来获得用户许可,以在其上调用这些 API 。

由 Google 发起的典型 OAuth 2.0 隐式流会话具有以下特征: 以下流程:

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

处理授权请求

当您的 Action 需要通过 OAuth 2.0 隐式流程执行账号关联时, Google 会通过包含以下内容的请求将用户发送到您的授权端点: 以下参数:

授权端点参数
client_id 您分配给 Google 的客户 ID。
redirect_uri 此请求的响应发送到的网址。
state 将一个在 重定向 URI。
response_type 要在响应中返回的值的类型。对于 OAuth 2.0 隐式 则响应类型始终为 token。

例如,如果您的授权端点可通过 https://myservice.example.com/auth 访问, 请求可能如下所示:

GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token

为了让授权端点能够处理登录请求,请执行以下步骤:

  1. 验证 client_id 和 redirect_uri 值, 防止向意外或配置错误的客户端应用授予访问权限:

    • 确认 client_id 是否与您的客户端 ID 匹配 分配给 Google。
    • 确认 redirect_uri 指定的网址 参数的格式如下:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      YOUR_PROJECT_ID 是项目设置页面上的 ID Actions 控制台界面。
  2. 检查用户是否已登录您的服务。如果用户未登录 中,完成服务的登录或注册流程。

  3. 生成 Google 将用于访问您的 API 的访问令牌。通过 访问令牌可以是任何字符串值,但必须唯一地表示 令牌对应的用户和客户端,且不得被猜到。

  4. 发送 HTTP 响应,将用户浏览器重定向到相应网址 由 redirect_uri 参数指定。添加所有 以下参数:

    • access_token:您刚刚生成的访问令牌
    • token_type:字符串 bearer
    • state:原始状态的未修改状态值 请求 以下是生成的网址示例:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE_STRING

Google 的 OAuth 2.0 重定向处理程序将收到访问令牌并确认 state 值没有更改。在 Google 获得 访问令牌,则 Google 会将该令牌附加到后续调用 作为 AppRequest 的一部分添加到您的 Action。

Design the voice user interface for the authentication flow

Check if the user is verified and start the account linking flow

  1. Open your Actions Builder project in the Actions Console.
  2. Create a new scene to start account linking in your Action:
    1. Click Scenes.
    2. Click the add (+) icon to add a new scene.
  3. In the newly created scene, click the add icon for Conditions.
  4. Add a condition that checks if the user associated with the conversation is a a verified user. If the check fails, your Action can't perform account linking during the conversation, and should fall back to providing access to functionality that doesn't require account linking.
    1. In the Enter new expression field under Condition, enter the following logic: user.verificationStatus != "VERIFIED"
    2. Under Transition, select a scene that doesn't require account linking or a scene that is the entry point to guest-only functionality.

  1. Click the add icon for Conditions.
  2. Add a condition to trigger an account linking flow if the user doesn't have an associated identity.
    1. In the Enter new expression field under Condition, enter the following logic:: user.verificationStatus == "VERIFIED"
    2. Under Transition, select the Account Linking system scene.
    3. Click Save.

After saving, a new account linking system scene called <SceneName>_AccountLinking is added to your project.

Customize the account linking scene

  1. Under Scenes, select the account linking system scene.
  2. Click Send prompt and add a short sentence to describe to the user why the Action needs to access their identity (for example "To save your preferences").
  3. Click Save.

  1. Under Conditions, click If user successfully completes account linking.
  2. Configure how the flow should proceed if the user agrees to link their account. For example, call the webhook to process any custom business logic required and transition back to the originating scene.
  3. Click Save.

  1. Under Conditions, click If user cancels or dismisses account linking.
  2. Configure how the flow should proceed if the user doesn't agree to link their account. For example, send an acknowledging message and redirect to scenes that provide functionality that doesn't require account linking.
  3. Click Save.

  1. Under Conditions, click If system or network error occurs.
  2. Configure how the flow should proceed if the account linking flow can't be completed because of system or network errors. For example, send an acknowledging message and redirect to scenes that provide functionality that doesn't require account linking.
  3. Click Save.

Handle data access requests

If the Assistant request contains an access token, first check that the access token is valid (and not expired) and then retrieve the associated user account from your database.