AmazonAlexa中帐户链接的python示例代码

2024-10-04 01:23:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在哪里可以找到AmazonAlexa中帐户链接的示例Python代码。我只能在这里得到文件

https://developer.amazon.com/docs/account-linking/understand-account-linking.html

请帮帮我


Tags: 文件代码httpscom示例docsdeveloperamazon
3条回答

我们可以使用ASK SDK for python中的函数get_account_linking_access_token()来获取用于帐户链接的用户令牌 并存储在变量account_linking_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

帐户链接对所有语言的工作方式相同,您应该熟悉OAuth2以在开发人员门户中配置帐户链接

Users can link account in two ways:

  1. From the skill detail card in the Alexa app while enabling the skill.
  2. From a link account card in the Alexa app after making a request that requires authentication.

当您将帐户与您的技能链接时,来自该技能的每个后续请求都将包含一个访问令牌。然后可以使用此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的代码外,链接帐户过程中不涉及任何编码

发送账户链接卡

在您的回复中,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'))

相关问题 更多 >