--- title: "6. Alexa Account Linking 设置" date: 2019-09-05T23:06:51+08:00 draft: false chapter: false weight: 206 --- 这章节描述如何在 *开发者账户系统* 和 *Amazon 账户系统* 之间建立 **Account Linking**。通过Account Linking可以使得Alexa区分出特定的用户,类似于登录功能,实现一些对用户级别的操作。 在该实验中,Amazon Cognito user Pool 是开发者账户系统,通过使用它,开发者可以快速建立 Account Linking 的关系,而且并不需要写任何代码。 有关 Amazon Cognito User Pool 的更多材料,请参考 [Developer Guide](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html) ## 什么是 Account Linking {{% notice note %}} 如果您已经什么是 Account Linking, 或者不关心原理,可以直接跳转到 [配置 App Client OAuth 2.0]({{< relref "#配置-app-client-oauth-2-0" >}}) {{% /notice %}} 以下是一段摘录自[Alexa Docs](https://developer.amazon.com/docs/account-linking/understand-account-linking.html#account-linking-and-the-skill-model) 的关于 Account Linking 的解释。 > You would use account linking if your skill needs personalized data from another system. > For example, suppose you own a web-based service "Car Fu" that lets users order taxis. > A custom skill that lets users access "Car Fu" by voice would be very useful. For example, > "Alexa, ask "Car Fu" to order a taxi." Completing this request requires the skill to access > your "Car Fu" service as a specific "Car Fu" user for profile and payment information. Therefore, > you need a link between the Amazon account used with the Alexa device and the "Car Fu" account > for the user. 在 Alexa Skills Kit 中,Account Linking 遵循 [OAuth 2.0](https://tools.ietf.org/html/rfc6749). 下图展示了如何获取 **AccessToken** 的流程. ![Auth-Code-Flow](/images/smart-home/auth-code-flow.png) Alexa 会在接下来所有的指令中,都附加上 **AccessToken**. 在 Alexa 后端 Lambda 中,程序需要解码并验证 **AccessToken** 来获得 用户有关的信息。 ![Flow](/images/smart-home/skill-interaction-flow.png) ## 配置 App Client OAuth 2.0 默认情况下, OAuth 2.0 在 Cognito User Pool 的 App Client 中是不激活的。按以下步骤来激活 OAuth 2.0. 1. 打开 [Cognito User Pool 控制台](https://console.aws.amazon.com/cognito/users/?region=us-east-1), 并选择已经创建 的用户池 1. 在左侧导航栏中, 位于 App integration 中, 选择 **App client settings** 1. 找到在 [创建 Cognito User Pool App Client]({{< ref "5.create-cup-client.zh.md" >}}) 中创建的 App Client. 如果您按照 教程执行,它应该叫做 `Alexa` 1. 在 **Enabled Identity Providers** 中,勾选 **Cognito User Pool** 1. 在 **Callback URL(s)** 中,输入从 Alexa Developer Console 复制的 **Redirect URLs**. 在 Alexa Developer Console 中,选择 **Account Linking**, 滚动到最下方,您可以找到三个 **Redirect URL**. Alexa 会根据用户的注册地域来重定向到不同的 URL。为了 服务所有的 Alexa 用户,建议拷贝所有的 URL 1. 在 **Allowed OAuth Flows** 中,勾选 **Authorization code grant** 1. 在 **Allowed OAuth Scopes** 中,勾选 **openid** 1. 点击 **Save changes** ![](/images/smart-home/configure-cup-oauth.png) ## 配置 Cognito User Pool 域名 默认的 Cognito 域名应该满足格式 `https://.auth..amazoncognito.com`。您可以使用自定义域名, 想了解更多关于 Cognito 域名的信息,请参考 [Adding a Custom Domain to a User Pool](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-add-custom-domain.html#cognito-user-pools-add-custom-domain-adding)。 在这个实验中,我们将使用默认域名格式。 1. 打开 Cognito User Pool 控制台 1. 在左侧导航栏,在 **App integration**中,选择 **Domain name** 1. 输入域名前缀,并且点击 **Check availability**, 这个域名名称必须是唯一的 1. 当提示说 "the domain is available",选择 **Save changes** ![](/images/smart-home/cognito-domain.png) ## 在 Alexa Developer 控制台中配置 Account Linking 1. 打开 [Alexa Console](https://developer.amazon.com/alexa/console/ask) 1. 在 **Skills** 列表中,选择之前创建的 Skill 1. 在左侧导航栏中,选择 **Account Linking** 1. 在 **Security Provider Information** 下,选择 **Auth Code Grant** 1. 在 **Authorization URI** 中输入 `https:///oauth2/authorize` 1. 在 **Access Token URI** 中输入 `https:///oauth2/token` 1. 输入 **Client ID** 和 **Client Secret**, 您可以在 Cognito User Pool 控制台的 **App Clients** 中找到他们。 ![](/images/smart-home/find-client-credentials.png) 1. 点击 **Add scope** 并且输入 `openid`. 对于 Smart Home Skill, 至少需要选择一个 scope 1. 在右上角点击 **Save** 按钮 ![](/images/smart-home/configure-alexa-account-linking.png) 关于 Cognito OAuth2.0 URI 的信息,请参考 [Amazon Cognito User Pools Auth API Reference](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-userpools-server-contract-reference.html). ## 在 Alexa App 中激活 Account Linking {{% notice info %}} 只有开发者账号才能在 APP 中看到正在开发中的账号。你必须使用创建 Skill 相同的账号来登陆 Alexa APP. 如果您位于中国大陆地区,您可能需要 使用 VPN 来访问。 {{% /notice %}} 1. 在手机上使用 Alexa APP 1. 在 APP 左上角点击 **汉堡按钮** 1. 选择 **Skills & Games**, 点击 **Your Skills** 1. 点击 **DEV**,你可以需要移动滑动窗来选择 **DEV** 1. 之前创建的 Smart Home Skill **SmartLamp**应该显示在列表中,点击技能来继续下面的操作 1. 点击 **Enable To Use** 按钮 1. 在弹出的窗口中, 输入您的 **email** and **password** 1. 账户关联建立成功 ![](/images/smart-home/account-linking-success.jpeg) 至此,Alexa 账户已经和 Cognito User Pool 账户建立起关联。Alexa 在后续发送给 AWS Lambda 的消息体中均会包含用户的 **accessToken**, 该 accessToken 为 JWT 格式。Alexa 发送的 JWT token 中的 sub 字段就是 Cognito User Pool 中的用户名。在 后端的 Lambda 中,您需要解码并验证这个 JWT token 来获取用户标识。 ## 参考文档 [Understand Account Linking](https://developer.amazon.com/docs/account-linking/understand-account-linking.html) [The OAuth2.0 Authorization Framework](https://tools.ietf.org/html/rfc6749) [JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token) [AWS Cognito User Pool](https://docs.aws.amazon.com/zh_cn/cognito/latest/developerguide/cognito-user-identity-pools.html)