question

newuser-6359bde1-7a23-43bc-a7e3-6169db978c57 avatar image
newuser-6359bde1-7a23-43bc-a7e3-6169db978c57 asked ·

Unable to get Access token using authorization code for a skill in lambda function

Hi,
I have successfully enabled send alexa event permission and got client_id and secret.
I had received an authgrant request which contains an authorization code for my lambda function. from there I'm trying to get an access token using `https://api.amazon.com/auth/o2/token` Getting below error
```{
"statusCode": 500,
"body": {},
"headers": {
"server": "Server",
"date": "Fri, 20 Nov 2020 03:37:03 GMT",
"content-type": "application/json;charset=UTF-8",
"content-length": "2",
"connection": "close",
"x-amz-rid": "REBDS5MD3K1X1F6TPTC3",
"x-amzn-requestid": "24855146-25ed-4e0a-8393-8802e776bc88",
"x-amz-date": "Fri, 20 Nov 2020 03:37:03 GMT",
"x-amzn-errortype": "InternalFailure:http://internal.amazon.com/coral/com.amazon.coral.service/",
"cache-control": "no-cache, no-store, must-revalidate",
"pragma": "no-cache",
"vary": "Content-Type,Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "api.amazon.com",
"port": 443,
"hostname": "api.amazon.com",
"hash": null,
"search": "?grant_type=authorization_code&code=RHpeYchXfMSMpbUJJYSR&client_id=amzn1.application-oa2-client.40acc86a12a2448cbe29311c6045f3ca&client_secret=d8c4e2befa71b255c7dae925bf7a4a150f767600cd6ce6de29cbd955ba2274ac",
"query": "grant_type=authorization_code&code=RHpeYchXfMSMpbUJJYSR&client_id=amzn1.application-oa2-client.40acc86a12a2448cbe29311c6045f3ca&client_secret=d8c4e2befa71b255c7dae925bf7a4a150f767600cd6ce6de29cbd955ba2274ac",
"pathname": "/auth/o2/token",
"path": "/auth/o2/token?grant_type=authorization_code&code=RHpeYchXfMSMpbUJJYSR&client_id=amzn1.application-oa2-client.40acc86a12a2448cbe29311c6045f3ca&client_secret=d8c4e2befa71b255c7dae925bf7a4a150f767600cd6ce6de29cbd955ba2274ac",
"href": "https://api.amazon.com/auth/o2/token?grant_type=authorization_code&code=RHpeYchXfMSMpbUJJYSR&client_id=amzn1.application-oa2-client.40acc86a12a2448cbe29311c6045f3ca&client_secret=d8c4e2befa71b255c7dae925bf7a4a150f767600cd6ce6de29cbd955ba2274ac"
},
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"accept": "application/json",
"content-length": 0
}
}
}
```
while referring amazon documentation when we enable send alexa permission a security profile will be created in developer console but, I'm not able to see in my console Reference: https://developer.amazon.com/en-US/docs/alexa/smarthome/authenticate-a-customer-permissions.html
Is there any change in the process?

alexa skills kitalexaalexa smart homesmart home
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Anand@Amazon avatar image
Anand@Amazon answered ·

Hello there,

Can you try below code and replace placeholder with your value. Remember auth code expires after 5 mins.

Code

var request = require('request');
var options = {
  'method': 'POST',
  'url': 'https://api.amazon.com/auth/o2/token',
  'headers': {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  },
  form: {
    'grant_type': 'authorization_code',
    'code': '<Auth code>', // Auth code sent to your lambda when you enable your skill.
    'client_id': '<Client ID value from permission section once send event gateway enabled>',
    'client_secret': '<Client ID value from permission section once send event gateway enabled>'
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Let me know if you still get error after following above suggestion.

10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.