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

How to send to Amazon's Alexa Event Gateway?

我正在尝试使用 Postman 将事件发送到 Amazon 的事件网关 以获取我的 Alexa Smart Home 技能,但我一直收到 'invalid access token exception.' 我已阅读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 将 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' 类型放入 header 中,并放入 [=45 的 'scope' =] object.

POST https://api.amazonalexa.com/v3/events?Content-Type=application/json&charset=UTF-8 header 指定 Bearer Token(之前收到的访问令牌)和包含以下内容的 body:

{
    "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."
    }
}

我想通了这个问题。我错误地发送了参数:Content-Type=application/jsoncharset=UTF-8 并将它们包含在 header - 我的错。您只需将它们包含在 header 中。