【发布时间】:2018-07-16 08:59:44
【问题描述】:
我在哪里可以找到 Amazon Alexa 中帐户链接的示例 Python 代码。我只能在这里获得文档。
https://developer.amazon.com/docs/account-linking/understand-account-linking.html
请帮帮我!!
【问题讨论】:
标签: python alexa alexa-skills-kit ask-sdk
我在哪里可以找到 Amazon Alexa 中帐户链接的示例 Python 代码。我只能在这里获得文档。
https://developer.amazon.com/docs/account-linking/understand-account-linking.html
请帮帮我!!
【问题讨论】:
标签: python alexa alexa-skills-kit ask-sdk
帐户链接对所有语言的工作方式都相同,您应该熟悉OAuth2 以在开发人员门户中配置帐户链接。
用户可以通过两种方式关联帐号:
- 在启用技能时从 Alexa 应用中的技能详细信息卡。
- 在发出需要身份验证的请求后,通过 Alexa 应用中的关联帐户卡。
当您将帐户与您的技能关联时,该技能的每个后续请求都将包含一个访问令牌。然后,您可以使用此accessToken 获取关联帐户的关联数据。
"session": {
"new": true,
"sessionId": "amzn1.echo-api.session.xxxxxxxxxxx",
"application": {
"applicationId": "amzn1.ask.skill.xxxxxxxxxx"
},
"user": {
"userId": "amzn1.ask.account.xxxxxxx",
"accessToken": "xxxxxxxxxxxxxx"
对于经过身份验证的用例,请始终检查accessToken 是否可用,如果请求中没有accessToken,则表示用户未通过身份验证,您可以向用户发送Account Link Card。除了发送Account Link card 的代码外,link-an-account 过程中不涉及任何编码。
发送账户链接卡:
在您的响应 JSON 中包含 LinkAccount 卡片
...
"outputSpeech": {
"type": "SSML",
"ssml": "<speak> Please link your account </speak>"
},
"card": {
"type": "LinkAccount"
}
...
【讨论】:
要在 Python 中发送帐户链接卡……
from ask_sdk_model.ui import Card
…
handler_input.response_builder.set_card(Card('LinkAccount'))
【讨论】:
我们可以使用 ASK SDK for python 中的函数get_account_linking_access_token() 来获取用于帐户链接的用户令牌
并存储在变量account_linking_token 中。账号绑定成功后,使用token获取用户数据,如下图:
from ask_sdk_model.ui import SimpleCard
speech_output = ''
if account_linking_token is not None:
url = "https://api.amazon.com/user/profile?access_token{}"\
.format(account_linking_token)
user_data = requests.get(url).json()
# retrieve the required user info here and populate output
# speech_output = ...
else:
# output msg when account linking is not done
# speech_output = ...
# return this response from the intent handler function
response = handler_input.response_builder
.speak(speech_output)
.ask(reprompt)
.set_card(SimpleCard(speech_output))
.response
【讨论】: