Account linking with OAuth

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

In the implicit code flow, Google opens your authorization endpoint in the user's browser. After successful sign in, you return a long-lived access token to Google. This access token is now included in every request sent from the Assistant to your Action.

In the authorization code flow, you need two endpoints:

  • The authorization endpoint, which is responsible for presenting the sign-in UI to your users that aren't already signed in and recording consent to the requested access in the form of a short-lived authorization code.
  • The token exchange endpoint, which is responsible for two types of exchanges:
    1. 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.
    2. 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.

Although the implicit code flow is simpler to implement, Google recommends that access tokens issued using the implicit flow never expire, because using token expiration with the implicit flow forces the user to link their account again. If you need token expiration for security reasons, you should strongly consider using the auth code flow instead.

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_idredirect_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.