Friday 2 June 2017

post is not working in Alexa

I'm writing a skill that does the below.

  1. Link the user account.
  2. Get the access token (a jwt).
  3. decode the token and get the userName.
  4. post this userName and the access token to another uri.
  5. Get the actual user data as response.

Currently I'm able to go till 3rd step mentioned above. I'm unable to post the data. below is my code.

function getUserDetailsFromAccessToken(session) {
    var token = session.user.accessToken;
    var decoded;
    try {
        // parse this and get user attributes
        decoded = jwt.decode(token);
    } catch (err) {
        console.log(err);
    }
    getTheUserProfile(decoded.firstname, token);
}
function getTheUserProfile(userFirstName, token) {
    var jsonToPass = {
        "accesstoken": token,
        "domain": "myDomain",
        "lanid": userFirstName
    };
    var options = {
        uri: "https://myUrl/api/admin/GetUserInfo",
        method: "POST",
        json: jsonToPass
    };
    console.log("Started");
    console.log(jsonToPass);
    request(options, function (error, resp, body) {
        console.log("Status code " + resp.statusCode);
        if (!error && resp.statusCode == 200) {
            if (body) {
                console.log(body);
            } else {
                console.log("I am unable to authenticate you. please disable the skill and re link your account");
            }
        } else {
            console.log(error);
        }
    });
    console.log("Ended");
}

when I run the above code, the output that I get is

2017-06-02T07:35:57.573Z 1d4de5dd-4766-11e7-ac8f-bd9aa01f674f Started 2017-06-02T07:35:57.573Z 1d4de5dd-4766-11e7-ac8f-bd9aa01f674f { accesstoken: 'myJwtToken',domain: 'myDomain',userId: 'userId' } 2017-06-02T07:35:57.583Z 1d4de5dd-4766-11e7-ac8f-bd9aa01f674f Ended

The issue is that the code never enters the request block.Thought that this is a problem at my end, so tried it out in my local, to my surprise this thing worked perfectly. Below is my local code.

var request = require("request");
var jwt = require('jsonwebtoken');
function getUserDetailsFromAccessToken(tokenFromSession) {
    var token = tokenFromSession;
    var decoded;
    try {
        // parse this and get user attributes
        decoded = jwt.decode(token);
    } catch (err) {
        console.log(err);
    }
    getTheUserProfile(decoded.firstname, token);
}
function getTheUserProfile(userFirstName, token) {
    var jsonToPass = {
        "accesstoken": token,
        "domain": "myDomain",
        "userId": userFirstName
    };
    var options = {
        uri: "https://myUrl/api/admin/GetUserInfo",
        method: "POST",
        json: jsonToPass
    };
    request(options, function (error, resp, body) {
        console.log("Status code " + resp.statusCode);
        if (!error && resp.statusCode == 200) {
            if (body) {
                console.log(body);
            } else {
                console.log("I am unable to authenticate you. please disable the skill and re link your account");
            }
        } else {
            console.log(error);
        }
    });
}
getUserDetailsFromAccessToken(myAccessToken); 



via user3872094

No comments:

Post a Comment