我想使用向网站发出一个GET请求。我正在尝试执行本文中最后一个代码块中描述的内容:https://developer.amazon.com/blogs/post/Tx3CX1ETRZZ2NPC/Alexa-Account-Linking-5-Steps-to-Seamlessly-Link-Your-Alexa-Skill-with-Login-wit (本文的末尾),但它没有发生。我的回调函数似乎从来没有被调用过。
我已经添加了所需的context.succeed(),实际上是它的最新版本,但仍然没有得到结果。我知道url很好,因为我可以将它复制/粘贴到浏览器中,并返回预期的结果。
下面是在回调中使用适当的上下文函数调用的答案,我已经尝试过了。Why is this HTTP request not working on AWS Lambda?
我不使用VPC。
我做错了什么?我觉得自己是个白痴,因为我一直在研究这个问题,并尝试了两天的解决方案。我记录完整的URL,当我从日志文件中复制/粘贴它,并将它放在浏览器窗口中时,我确实得到了一个有效的结果。谢谢你的帮助。
以下是代码:
function getUserProfileInfo(token, context) {
console.log("IN getUserProfileInfo");
var request = require('request');
var amznProfileURL = 'https://api.amazon.com/user/profile?access_token=';
amznProfileURL += token;
console.log("calling it");
console.log(amznProfileURL);
console.log("called it");
request(amznProfileURL, function(error, response, body) {
if (!error && response.statusCode == 200) {
var profile = JSON.parse(body);
console.log("IN getUserProfileInfo success");
console.log(profile);
context.callbackWaitsForEmptyEventLoop = false;
callback(null, 'Success message');
} else {
console.log("in getUserProfileInfo fail");
console.log(error);
context.callbackWaitsForEmptyEventLoop = false;
callback('Fail object', 'Failed result');
}
});
console.log("OUT getUserProfileInfo");
}这是我在CloudWatch中获得的日志输出:
2017-03-08T22:20:53.671Z 7e393297-044d-11e7-9422-39f5f7f812f6 getUserProfileInfo 2017-03-08T22:20:53.728Z 7e393297-044d-11e7-9422-39f5f7f812f6退出getUserProfileInfo
发布于 2017-03-19 02:15:48
问题可能是您使用的是var request = require('request');,它是外部依赖的,需要您进行打包的lambda部署才能工作。有关信息,请参阅此答案。
另一种方法是,您可以使用NodeJS模块,例如内置模块var http= require('http');来进行请求。这样,您就可以进行简单的lambda脚本部署。
参考文献
http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html
https://stackoverflow.com/questions/42700630
复制相似问题