javascript - AWS Lambda 中的 HTTP GET 只能运行一次

标签 javascript http get aws-lambda alexa-skills-kit

我尝试使用 Amazon 用户配置文件 API 查找用户配置文件信息,但我的 GET 请求仅有效一次。第一次,配置文件信息返回没有问题,但所有后续调用 lambda 函数都会导致 GET 返回 400 错误请求。这是我当前的代码:

exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
// console.log(event);
if (event.session.user.accessToken === undefined) {
    alexa.emit(':tellWithLinkAccountCard',
                  'To start using the app, please use the Alexa companion app to authenticate on Amazon');
} else {
    amazonProfileURL += event.session.user.accessToken;
    console.log(amazonProfileURL);
    http.get(amazonProfileURL, (res) => {
      const statusCode = res.statusCode;
      const contentType = res.headers['content-type'];

      let error;
      if (statusCode !== 200) {
        error = new Error(`Request Failed.\n` +
                          `Status Code: ${statusCode}`);
      } else if (!/^application\/json/.test(contentType)) {
        error = new Error(`Invalid content-type.\n` +
                          `Expected application/json but received ${contentType}`);
      }
      if (error) {
        console.log(error.message);
        // consume response data to free up memory
        res.resume();
        return;
      }

      res.setEncoding('utf8');
      let rawData = '';
      res.on('data', (chunk) => rawData += chunk);
      res.on('end', () => {
        try {
            let parsedData = JSON.parse(rawData);
            user_name = parsedData.name;
            user_email = parsedData.email;
            console.log(user_name);
            console.log(user_email);
            alexa.registerHandlers(handlers);
            alexa.execute();
        } catch (e) {
          console.log(e.message);
        }
      });
    }).on('error', (e) => {
      console.log(`Got error: ${e.message}`);
    });

第一次调用该函数时,代码成功运行 alexa.execute() 函数,但在所有后续调用中,以下内容都会打印到控制台:

START RequestId: 89a3a696-1442-11e7-9c15-6de8cce4b94d Version: $LATEST
2017-03-29T05:42:46.772Z    89a3a696-1442-11e7-9c15-6de8cce4b94d    https://api.amazon.com/user/profile?access_token=<long user access token>
2017-03-29T05:42:46.888Z    89a3a696-1442-11e7-9c15-6de8cce4b94d    Request Failed.
Status Code: 400

我对 JavaScript 和 AWS Lambda 相当陌生,因此非常感谢您的帮助!

最佳答案

我解决了这个问题。事实证明,这是一个 AWS Lambda 问题(功能?),而不是 HTTP GET 问题。我的 amazonProfileURL 字符串在 lambda 函数调用之间持续存在,因此它只是第一次调用的有效 URL。解决方案是将该行更改为 requestURL = amazonProfileURL + event.session.user.accessToken,以便 amazonProfileURL 不会被覆盖。

关于javascript - AWS Lambda 中的 HTTP GET 只能运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43085336/

相关文章:

python - 使用 scrapyd api 为蜘蛛提供 url

http - RESTful View 计数器

php - 如何在查询字符串中将一些值传递给我的 facebook canvas 应用程序?

javascript - 如何在 Angular 2+ 中获取 json 数据

javascript - 可以修改 jQuery ajax 请求中的 cookie 值吗?

javascript - 检查 gitlab 项目中文件的更新

javascript - 以正确的顺序执行 Promise 的 For 循环

c# - 在 C# 中设置和获取

javascript - $.get 中的 setState

javascript - 如何安装 NPM 拒绝安装的依赖项?