dimanche 28 juin 2015

Verify Amazon Login Access Token in AWS Lambda Call

I am trying to verify the access token returned from Login with Amazon in a AWS Lambda Call.

According to the docs I need to:

To verify a token, make a secure HTTP call to http://ift.tt/1IlCm8D, passing the access token you wish to verify. You can specify the access token as a query parameter. For example:

http://ift.tt/1GFxKof!.....

Note Access tokens contain characters that are outside the allowed range for URLs. Therefore, you should URL encode access tokens to prevent errors.

In the lambda function I have the following code

var http = require('http');
var querystring = require('querystring');

exports.handler = function(event, context) {

var postData = querystring.stringify({});

var options = {
    hostname: 'api.amazon.com',
    port: 443, // 443 for https // with 80 get ECONNREFUSED // with 443 ECONNRESET
    path: '/auto/O2/tokeinfo?access_token=' + event.token,
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': postData.length
    }
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
    res.on('end', function() {
        context.succeed("hello");  
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    context.fail('problem with request: ' + e.message); 
});

// write data to request body
req.write(postData);
req.end();

// context.fail('Something went wrong');
};

Unfortunately with this I am getting back

{
    "errorMessage": "problem with request: read ECONNRESET"
}

If I change the port 443 to port 80 I get back ECONNREFUSED. If I don't send the req.write(postData); the Lambda function times out.

Is there another message on the res.on I need to be listening for?




Aucun commentaire:

Enregistrer un commentaire