通过 OAuth (Dialogflow) 进行帐号关联

OAuth 关联类型支持两种业界标准 OAuth 2.0 流程,即隐式和授权代码流程。

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.

实现 OAuth 账号关联

配置项目

如需将项目配置为使用 OAuth 账号关联,请按以下步骤操作:

  1. 打开 Actions 控制台,然后选择您要使用的项目。
  2. 点击开发标签页,然后选择帐号关联
  3. 启用帐号关联旁边的开关。
  4. “帐号创建”部分中,选择不,我只希望在我的网站上创建帐号

  5. 在“关联类型”中,依次选择 OAuth隐式

  6. 客户信息中:

    • 您的 Actions 向 Google 发出的客户端 ID 分配一个值,以标识来自 Google 的请求。
    • 插入授权端点和令牌交换端点的网址。
  1. 点击保存

实现 OAuth 服务器

为了支持 OAuth 2.0 隐式流程,您的服务会通过 HTTPS 提供授权端点。此端点负责验证数据访问并从用户那里获得同意。授权端点会向尚未登录的用户呈现登录界面,并记录用户同意所请求的访问。

当您的 Action 需要调用某项服务的已授权 API 时,Google 会使用此端点从您的用户处获取权限,以便代表他们调用这些 API。

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

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

处理授权请求

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

授权端点参数
client_id 您分配给 Google 的客户端 ID。
redirect_uri 您要将该请求的响应发送到的网址。
state 在重定向 URI 中原封不动地传回 Google 的簿记值。
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 与您分配给 Google 的客户端 ID 匹配。
    • 确认 redirect_uri 参数指定的网址采用以下格式:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      YOUR_PROJECT_ID 是在 Actions 控制台的 Project settings 页面上找到的 ID。
  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 为您的服务获取访问令牌后,会将该令牌作为 AppRequest 的一部分附加到对您的 Action 的后续调用。

启动身份验证流程

使用帐号登录帮助程序 intent 启动身份验证流程。以下代码段说明了如何在 Dialogflow 和 Actions SDK 中发送响应以使用此帮助程序。

Dialogflow:

Node.js
const {dialogflow, SignIn} = require('actions-on-google');
const app = dialogflow({
  // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
  clientId: CLIENT_ID,
});
// Intent that starts the account linking flow.
app.intent('Start Signin', (conv) => {
  conv.ask(new SignIn('To get your account details'));
});
Java
@ForIntent("Start Signin")
public ActionResponse text(ActionRequest request) {
  ResponseBuilder rb = getResponseBuilder(request);
  return rb.add(new SignIn().setContext("To get your account details")).build();
}
JSON 文件
{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "richResponse": {
        "items": [
          {
            "simpleResponse": {
              "textToSpeech": "PLACEHOLDER"
            }
          }
        ]
      },
      "userStorage": "{\"data\":{}}",
      "systemIntent": {
        "intent": "actions.intent.SIGN_IN",
        "data": {
          "@type": "type.googleapis.com/google.actions.v2.SignInValueSpec",
          "optContext": "To get your account details"
        }
      }
    }
  },
  "outputContexts": [
    {
      "name": "/contexts/_actions_on_google",
      "lifespanCount": 99,
      "parameters": {
        "data": "{}"
      }
    }
  ]
}

Actions SDK:

Node.js
const {actionssdk, SignIn} = require('actions-on-google');
const app = actionssdk({
  // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
  clientId: CLIENT_ID,
});
// Intent that starts the account linking flow.
app.intent('actions.intent.TEXT', (conv) => {
  conv.ask(new SignIn('To get your account details'));
});
Java
@ForIntent("actions.intent.TEXT")
public ActionResponse text(ActionRequest request) {
  ResponseBuilder rb = getResponseBuilder(request);
  return rb.add(new SignIn().setContext("To get your account details")).build();
}
JSON 文件
{
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "PLACEHOLDER"
              }
            }
          ]
        }
      },
      "possibleIntents": [
        {
          "intent": "actions.intent.SIGN_IN",
          "inputValueData": {
            "@type": "type.googleapis.com/google.actions.v2.SignInValueSpec",
            "optContext": "To get your account details"
          }
        }
      ]
    }
  ],
  "conversationToken": "{\"data\":{}}",
  "userStorage": "{\"data\":{}}"
}

处理数据访问请求

如果 Google 助理请求包含访问令牌,请先检查访问令牌是否有效(且未过期),然后从数据库中检索关联的用户帐号。