json 如何发送到亚马逊的Alexa事件网关?

gv8xihay  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(99)

我试图测试发送事件到亚马逊的事件网关为我的Alexa智能家居技能使用 Postman ,但我一直收到一个“无效的访问令牌异常”.我已经阅读了Amazon's documentation在这方面,但显然我错过了一些东西.
当我启用技能时,我的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发送POST到'https://api.amazon.com/auth/o2/token'以接收 AccessRefresh 令牌。然后它存储这些令牌。接下来,我的Lamdba回应如下:

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

然后,我得到一个消息网页,我已经成功地链接我的技能-一切都很好.
接下来,我尝试使用Postman应用程序将事件发送到Amazon的Alexa事件网关。我将访问令牌(我也尝试了刷新令牌)作为“BearerToken”类型放在标头中,并放在“endpoint”对象的“scope”中。

POSThttps://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未授权”

{
    "header": {
        "namespace": "System",
        "name": "Exception",
        "messageId": "95bd23c3-76e6-472b-9c6d-74d436e1eb61"
    },
    "payload": {
        "code": "INVALID_ACCESS_TOKEN_EXCEPTION",
        "description": "Access token is not valid."
    }
}
5cnsuln7

5cnsuln71#

我发现问题所在了。我错误地发送了参数:Content-Type=application/jsoncharset=UTF-8以及将它们包含在头文件中-我的错。你只需要把它们包含在标题中。

相关问题