Validate and Use Access Tokens in Smart Home and Video Skill Code

Once a user successfully enables your smart home or video skill and links their Alexa account with your device cloud, requests sent to your skill include the user's access token (token). Add logic to the service for your skill to verify the token, then use it to access information about the user's devices in the Resource server

Get the Access Token from the Directive

When your skill receives a directive from Alexa, it includes an endpoint section that contains a scope. The scope contains a bearer token that you can use to authenticate the user in your system.

Following is an example endpoint that contains a bearer token:

"endpoint": {
    "scope": {
        "type": "BearerToken",
        "token": "access-token-from-skill"
    },
    "endpointId": "appliance-001",
    "cookie": {}
},

Retrieve the token from endpoint.scope.token.

Verify that the Token is Valid

Once you have the token, verify that it identifies a user in your resource server . The token could become invalid for multiple reasons, for example:

  • The user deleted or canceled their account with your service. For example, an Alexa user might have set up account linking with My Lights, then later canceled their My Lights account. At this point, the token stored by the Alexa service would identify a non-existent user.
  • The token has expired, and the Alexa service was unable to obtain a new token. This can occur with authorization code grant if your authorization server does not provide refresh tokens.

If the token is valid, handle the request normally. You can use the token to access data from your resource server as needed. In the My Lights example, the skill would access the device cloud and take an appropriate action based on the directive the user requested. For instance, if the user invoked the TurnOn directive, the skill would connect with the device cloud to turn on a light.

Respond to the User if the Token is Invalid or Expired

If the access token is invalid, return an ErrorResponse with the type set to one of the following:

  • EXPIRED_AUTHORIZATION_CREDENTIAL (for an expired token)
  • INVALID_AUTHORIZATION_CREDENTIAL (for an invalid token)

OAuth Resources:

Other Resources: