AWS Alexa技能中的无效访问令牌

时间:2019-07-24 13:34:08

标签: node.js async-await alexa-skill

这是我的代码,我使用alexa技能来获取个人资料详细信息,但出现401问题以及以下错误

const GetMyEmailIntentHandler = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
      handlerInput.requestEnvelope.request.intent.name === 'GetMyEmailIntent'
    );
  },
  async handle(handlerInput) {
            var apiaccessToken = handlerInput.requestEnvelope.context.System.apiAaccessToken;
            var options = {
                host :  baseURL,
                path : '/v2/accounts/~current/settings/Profile.email',

                Accept: 'application/json',
                method : 'GET',
                 headers:{
                    auth: 'Bearer ' + apiaccessToken            
                 }
            }            
          // making the https get call
            var getReq = https.request(options, function(res) {
                res.on('data', function(data) {
                });
            });

            //end the request
            getReq.end();
            getReq.on('error', function(err){
            }); 

    return new Promise(resolve => {
      getEmail(apiaccessToken => {
        var speechText = 'Your accessToken fetched successfully';
        resolve(
          handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse()
        );
      });
    });

  }
};

导致的错误消息是401错误,指出它无法确定域名。它还说我有一个无效的令牌。但是,我在选项对象中提供了auth承载令牌作为标头。我正在进行字符串串联,以使Bearer出现在handlerInput上的api令牌上。

2019-07-24T13:12:17.200Z    c3b8254b-e773-43db-8a96-0ff0aeea1f5e    Error handled: Unable to determine the domain name
2019-07-24T13:12:17.418Z    c3b8254b-e773-43db-8a96-0ff0aeea1f5e    
status code:============= 401
2019-07-24T13:12:17.419Z    c3b8254b-e773-43db-8a96-0ff0aeea1f5e    
INSIDE res.on:============= { code: 'ACCESS_DENIED', message: 'Invalid token' }
END RequestId: c3b8254b-e773-43db-8a96-0ff0aeea1f5e

1 个答案:

答案 0 :(得分:0)

我也一样。我所做的唯一更改是从Context获取了API端点,而不是对其进行硬编码。下面是对我有用的代码。

var profileAccessToken = 'Bearer ' + this.event.context.System.apiAccessToken;
var profileApiEndpoint = this.event.context.System.apiEndpoint;
const options = {
  Host: profileApiEndpoint,
  Accept: 'application/json',
  Authorization: profileAccessToken
};

console.log(options);
var requestURLProfileEmail = profileApiEndpoint + "/v2/accounts/~current/settings/Profile.email";
console.log(requestURLProfile);
https.get(requestURLProfileEmail, options, (resp) => {
  let data = '';
  resp.on('data', (chunk) => {
    data += chunk;
  });
  resp.on('end', () => {
    console.log('Response profile info request-->' + data);
  });
}).on("error", (err) => {
  console.log(err);
  this.emit(":tell", "There was an error processing your request.Please try again.");
});