PaulPerry
PaulPerry

Reputation: 906

How to call web service from Alexa Lambda function

I want to make a GET request to a web site using the Amazon Profile API. I am trying to do what is described in the last code chunk in this article: https://developer.amazon.com/blogs/post/Tx3CX1ETRZZ2NPC/Alexa-Account-Linking-5-Steps-to-Seamlessly-Link-Your-Alexa-Skill-with-Login-wit (very end of article) and it just does not happen. My callback function never seems to get called.

I have added the required context.succeed(), actually the latest version of that, and am still not getting results. I know the url is good, as I can take it and copy/paste into a browser and it returns an expected result.

Here is a SO answer on using the appropriate context function calls within the callback, which I have tried. Why is this HTTP request not working on AWS Lambda?

I am not using a VPC.

What am I doing wrong? I feel like a moron, as I have been researching this and trying solutions for 2 days. I log the full URL, and when I copy/paste that out of the log file, and put it in a browser window, I do get a valid result. Thanks for your help.

Here is the code:

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");

}

This is the logging output I get in CloudWatch:

2017-03-08T22:20:53.671Z 7e393297-044d-11e7-9422-39f5f7f812f6 IN getUserProfileInfo 2017-03-08T22:20:53.728Z 7e393297-044d-11e7-9422-39f5f7f812f6 OUT getUserProfileInfo

Upvotes: 1

Views: 1558

Answers (1)

bibek shrestha
bibek shrestha

Reputation: 448

Problem might be that you are using var request = require('request'); which is external dependency and will require you to make a packaged lambda deployment for it to work. See this answer for relevant information.

AWS Node JS with Request

Another way is that you can use NodeJS module such as var http= require('http'); which is builtin module to make the requests. This way you can just make plain lambda script deployment.

Reference

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html

Upvotes: 1

Related Questions