【问题标题】:Alexa access token ivalid scopeAlexa 访问令牌无效范围
【发布时间】:2020-10-05 11:49:13
【问题描述】:

我正在训练构建自己的 Alexa 技能,以从 Python 发送消息以打开/关闭设备。 我正在阅读官方文档 (https://developer.amazon.com/it-IT/docs/alexa/smapi/skill-messaging-api-reference.html) 但我无法检索令牌,因为我的范围无效。 我将附加配置屏幕 Account linking configuration 代码是

import requests

SKILL_ID = 'amzn1.ask.skill.xxxxx'
SKILL_CLIENT_ID = 'amzn1.application-oa2-client.xxxxx'
SKILL_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
API_TOKEN_URL = 'https://api.amazon.com/auth/O2/token'


def richiediToken():
    scope = "alexa:skill_messaging"
    payload = "grant_type=client_credentials&scope=" + scope + "&client_id=" + \
        SKILL_CLIENT_ID + "&client_secret=" + SKILL_CLIENT_SECRET
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    print("Header: ", headers)
    print("Body: ", payload)
    richiestaToken = requests.post(
        API_TOKEN_URL, data=payload, headers=headers)
    print("Risposta:")
    print(richiestaToken.json())


richiediToken()

为什么文档提供的范围不起作用? 谢谢你:)

【问题讨论】:

    标签: python-3.x oauth-2.0 alexa alexa-skills-kit alexa-skill


    【解决方案1】:

    您可能使用了错误的密钥。根据您已链接的doc,您应该从权限部分获取它:

    1. 在开发者控制台中打开技能列表,然后单击编辑您的技能。 2. 在 Build 选项卡上,单击左下角的 Permissions 部分。 3. 滚动到权限页面底部的 Alexa 技能消息部分。 4. 复制 Client Id 和 Client Secret 值以备后用。

    根据您共享的屏幕 - 您正在使用 Account Linking 选项卡中的值。

    您的代码与权限选项卡中的 clientId/secret 一起正常工作:

    λ docker run so-python
    Header:  {'content-type': 'application/x-www-form-urlencoded'}
    Body:  grant_type=client_credentials&scope=alexa:skill_messaging&client_id=amzn1.application-oa2-client.(...)&client_secret=(...)
    Risposta:
    {'access_token': 'Atc|MQEBI(...)', 'scope': 'alexa:skill_messaging', 'token_type': 'bearer', 'expires_in': 3600}
    

    稍后发送消息:

        token = richiestaToken.json()
        authorization = "Bearer " + list(token.values())[0] + ""
        print(authorization)
        messagePayload= {"data": {"sampleMessage": "Sample Message"}, "expiresAfterSeconds": 60}
        messageHeaders = {'Authorization': authorization, 'content-type': 'application/json'}
        messageResponse = requests.post(
            "https://api.amazonalexa.com/v1/skillmessages/users/amzn1.ask.account.AG4SB7...",
            json=messagePayload,
            headers=messageHeaders
            )
        print(messageResponse)
    

    它应该返回 202 HTTP 状态。

    【讨论】:

    • 感谢您的回答。在权限部分我没有技能消息部分....我必须启用它吗?如果是,怎么做?
    • " 注意:如果您没有看到 Alexa Skill Messaging 部分,请在页面上切换权限。然后会出现 Alexa Skill Messaging 部分。复制了 Client Id 和 Client Secret 值后,您然后可以根据需要更改权限。”,developer.amazon.com/en-US/docs/alexa/smapi/…
    • 谢谢你。我有 clientid 和 clientsecret 但由于范围而仍然无法工作。我需要在技能配置中设置范围吗?指南没有这么说……
    • 您在请求正文中声明要使用的范围。问题是 - 你想稍后使用获得的 access_token 调用哪个 api?
    • 我正在使用之前链接的文档。我想为令牌调用 api.amazon.com/auth/O2/token 并在 api.eu.amazonalexa.com/v1/skillmessages/users/{userId} 之后调用技能。该文档说在使用第二个链接之前使用第一个链接来检索令牌。我有什么错?
    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2023-03-15
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多