如何发送到亚马逊的Alexa Event Gateway?

问题描述 投票:0回答:1

我正在尝试使用 Postman 测试将事件发送到 Amazon 事件网关以获取 Alexa 智能家居技能,但我不断收到“无效访问令牌异常”。我已经阅读了亚马逊的文档,但显然我遗漏了一些东西。

当我启用技能时,我的智能家居 Lambda 会收到 AcceptGrant

{
    "directive": {
        "header": {
            "namespace": "Alexa.Authorization",
            "name": "AcceptGrant",
            "messageId": "b2862179-bc56-4bb2-ac05-ce55c7a3e977",
            "payloadVersion": "3"
        },
        "payload": {
            "grant": {
                "type": "OAuth2.AuthorizationCode",
                "code": "ANSVjPzpTDBsdfoRSyrs"
            },
            "grantee": {
                "type": "BearerToken",
                "token": "Atza|IwEB..."
            }
        }
    }
}

我的 lambda 向“https://api.amazon.com/auth/o2/token”发送 POST 以接收 AccessRefresh 令牌。然后它存储这些令牌。接下来,我的 Lamdba 响应如下:

{
    "event": {
        "header": {
            "namespace": "Alexa.Authorization",
            "name": "AcceptGrant.Response",
            "messageId": "b2862179-bc56-4bb2-ac05-ce55c7a3e977",
            "payloadVersion": "3"
        },
        "payload": {}
    }
}

然后我收到一个消息网页,表明我已成功链接我的技能 - 一切都很好。

接下来,我尝试使用 Postman 应用程序将事件发送到 Amazon 的 Alexa 事件网关。我将访问令牌(我还尝试了刷新令牌)作为“BearerToken”类型放在标头中,并将其放在“端点”对象的“scope”中。

POST https://api.amazonalexa.com/v3/events?Content-Type=application/json&charset=UTF-8 带有指定承载令牌(之前收到的访问令牌)的标头和包含以下内容的正文:

{
    "event": {
        "header": {
            "messageId": "abc-123-def-456",
            "namespace": "Alexa",
            "name": "ChangeReport",
            "payloadVersion": "3"
        },
        "endpoint": {
            "scope": {
                "type": "BearerToken",
                "token": "<access token>"
            },
            "endpointId": "MySmartSwitch-001"
        },
        "payload": {
            "change": {
                "cause": {
                    "type": "RULE_TRIGGER"
                },
                "properties": [
                    {
                        "namespace": "Alexa.ModeController",
                        "name": "mode",
                        "value": "Backup",
                        "timeOfSample": "2020-01-02T09:30:00ZZ",
                        "uncertaintyInMilliseconds": 50
                    }
                ]
            }
        }
    },
    "context": {
        "properties": [
            {
                "namespace": "Alexa.PowerController",
                "name": "powerState",
                "value": "ON",
                "timeOfSample": "2020-01-02T09:30:00Z",
                "uncertaintyInMilliseconds": 60000
            },
            {
                "namespace": "Alexa.EndpointHealth",
                "name": "connectivity",
                "value": {
                    "value": "OK"
                },
                "timeOfSample": "2020-01-02T09:30:00Z",
                "uncertaintyInMilliseconds": 0
            }
        ]
    }
}

收到的回复是“401 Unauthorized”

{
    "header": {
        "namespace": "System",
        "name": "Exception",
        "messageId": "95bd23c3-76e6-472b-9c6d-74d436e1eb61"
    },
    "payload": {
        "code": "INVALID_ACCESS_TOKEN_EXCEPTION",
        "description": "Access token is not valid."
    }
}
node.js json aws-lambda alexa-skills-kit alexa-smart-home-skill
1个回答
2
投票

我解决了这个问题。我错误地发送了参数: Content-Type=application/jsoncharset=UTF-8 以及将它们包含在标头中 - 我的错。您只需将它们包含在标题中即可。

© www.soinside.com 2019 - 2024. All rights reserved.