我正在尝试使用 Postman 测试将事件发送到我的 Alexa 智能家居技能的 Amazon 事件网关,但我不断收到“无效访问 token 异常”。我已经阅读了关于此的 Amazon's documentation 但显然我遗漏了一些东西。
当我启用技能时,我的智能家居 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 以接收访问和刷新 token 。然后它存储这些 token 。接下来,我的 Lamdba 响应如下:
{
"event": {
"header": {
"namespace": "Alexa.Authorization",
"name": "AcceptGrant.Response",
"messageId": "b2862179-bc56-4bb2-ac05-ce55c7a3e977",
"payloadVersion": "3"
},
"payload": {}
}
}
然后我收到一个消息网页,表明我已成功链接我的技能 - 一切都很好。
接下来,我尝试使用 Postman 应用程序将事件发送到 Amazon 的 Alexa 事件网关。我将访问 token (我还尝试了刷新 token )作为“BearerToken”类型放在 header 中,并将其放在“端点”对象的“范围”中。
发布 https://api.amazonalexa.com/v3/events?Content-Type=application/json&charset=UTF-8 带有指定承载 token (之前收到的访问 token )的 header 和包含以下内容的正文:
{
"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."
}
}
最佳答案
我发现了这个问题。我错误地发送了参数: Content-Type=application/json 和 charset=UTF-8 以及将它们包含在 header 中 - 我的错。您只需将它们包含在标题中即可。
关于node.js - 如何发送到亚马逊的Alexa Event Gateway?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59568366/