node.jsjsonaws-lambdaalexa-skills-kitalexa-smart-home-skill

How to send to Amazon's Alexa Event Gateway?


I am trying to test sending an event to the Amazon's Event Gateway for my Alexa Smart Home skill using Postman but I keep receiving an 'invalid access token exception.' I have read the Amazon's documentation on this but apparently I am missing something.

When I enable my skill, my Smart Home Lambda receives the 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..."
            }
        }
    }
}

My lambda sends a POST to 'https://api.amazon.com/auth/o2/token' to receive the Access and Refresh tokens. It then stores those tokens. Next, my Lamdba responds with the following:

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

I then get a message web page that I have successfully linked my skill - all is good.

Next, I try to send an event to Amazon's Alexa event gateway using the Postman app. I put the Access token (I also tried the Refresh token) in the header as a 'BearerToken' type and the in the 'scope' of the 'endpoint' object.

POST https://api.amazonalexa.com/v3/events?Content-Type=application/json&charset=UTF-8 with a header that specifies a Bearer Token (Access token received earlier) and a body that contains the following:

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

The response received is '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."
    }
}

Solution

  • I figured out the issue. I was mistakenly sending parameters: Content-Type=application/json and charset=UTF-8 as well including them in the header - my bad. You just need to include them in the header.