1

When developing the Alexa skill. I'm trying to use Axios get to get the whole day's events information from google calendar api. When I redo the account linking, it works well. I thought the asscessToken could only last for 2 hours, and I get the 401 error message because the token is expired.

Here is my Axios code

const getEvents = async (url, accessToken) => {
  try {
    const config = {
      headers: {'Authorization': 'Bearer ' + accessToken}
    };
    return await Axios.get(url, config);

  } catch (error) {
    console.log('Error getting events');
    console.error(error);
  }
}

Code for my url and the asscesstoken

const url = EVENTS_URL + "?orderBy=updated&timeMin=" + timeMin + "&timeMax=" 
+ timeMax;
const accessToken = 
handlerInput.requestEnvelope.context.System.user.accessToken;

Error Message:

ERROR   { Error: Request failed with status code 401
    at createError (/var/task/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/var/task/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/var/task/node_modules/axios/lib/adapters/http.js:260:11)
    at IncomingMessage.emit (events.js:203:15)
    at IncomingMessage.EventEmitter.emit (domain.js:448:20)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

method: 'get',
     headers:
      { Accept: 'application/json, text/plain, */*',
        Authorization: 'Bearer undefined',
        'User-Agent': 'axios/0.21.1' }, 
Xiaoyi Liu
  • 11
  • 2
  • Your access token, where are you getting it from? – Aerials May 27 '21 at 10:23
  • Thanks for your reply. I am able to get access token in the lambda function const accesstoken =handlerInput.requestEnvelope.context.System.user.accessToken; – Xiaoyi Liu May 27 '21 at 11:37
  • See if this [Q/A](See here for pointers: https://stackoverflow.com/q/54439201/4243927) helps you configure the lambda function to get refresh tokens – Aerials May 27 '21 at 11:45
  • Thanks, do you think any bug in my code? Becasue If the user has successfully linked their account, the user's access token (accessToken) is included in each request sent to your skill. The token is available in the accessToken property of the user object, which is available in the context object. The context object is included in all requests. i can access the accessToken in context.System.user.accessToken. – Xiaoyi Liu May 27 '21 at 11:56
  • Based on the link you provided. I've configured Login with Amazon, and configured Account Linking for my Alexa skill. When I do the account linking my code works, but after 2 hours, my code with return 401 error message. tried enable/disable skill in companion app it will works. – Xiaoyi Liu May 27 '21 at 12:11

0 Answers0