【问题标题】:Invalid Access Token in AWS Alexa SkillAWS Alexa Skill 中的访问令牌无效
【发布时间】:2019-07-24 13:34:08
【问题描述】:

这是我的代码,用于使用 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 错误,指出无法确定域名。它还说我有一个无效的令牌。但是,我已将身份验证承载令牌作为选项对象内的标头提供。我正在执行字符串连接以将 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

【问题讨论】:

  • 标题应该是“授权”而不是“授权”
  • 同时将“Accept”移动到 headers 对象中。

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


【解决方案1】:

我也一样。我所做的唯一更改是从 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.");
});

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 2018-01-21
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2019-11-09
    • 1970-01-01
    相关资源
    最近更新 更多