我正在尝试使用 Postman 测试将事件发送到亚马逊的事件网关以获取我的 Alexa 智能家居技能,但我一直收到“无效的访问令牌异常”。我已经阅读了亚马逊的文档,但显然我遗漏了一些东西。
当我启用我的技能时,我的 Smart Home 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 以接收访问和刷新令牌。然后它存储这些令牌。接下来,我的 lamdba 响应如下:
{
"event": {
"header": {
"namespace": "Alexa.Authorization",
"name": "AcceptGrant.Response",
"messageId": "b2862179-bc56-4bb2-ac05-ce55c7a3e977",
"payloadVersion": "3"
},
"payload": {}
}
}
然后我得到一个消息网页,我已成功链接我的技能 - 一切都很好。
接下来,我尝试使用 Postman 应用程序向亚马逊的 Alexa 事件网关发送一个事件。我将访问令牌(我也尝试过刷新令牌)作为“BearerToken”类型放在标题中,并放在“端点”对象的“范围”中。
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."
}
}