-2

I am trying to send request to URL using xmlhttprequest and running the js file using Node js. but the response to it is coming as undefined. when i post the the same data using CURL command, i am getting proper response. Why is it not working in case of nodejs and xmlhttprequest.

Here is the code i have written. file name is test.js

   'use strict';


var readlineSync = require('readline-sync');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var urlopen =require("openurl");


var clientId = "amzn1.application-oa2-client.775a44579eaf461b92db3d1a4cb23a5a";

var deviceId = "Test_device1";
var deviceSerialNumber = 123;
var redirectUri = "https://localhost:9745/authresponse";
var responseType ="code";

var clientSecret = "29a6520d97d11d640e030786e133ccec9ead67005aaa45c212e72e10b00900ff";
promptUserLogin();


sleep(10000);

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}


var Urlcode = readlineSync.question('enter the code ');

var proceed = readlineSync.question('proceed with execution?');
if(proceed){
  getTokenFromCode(Urlcode); 
}
function promptUserLogin() {


    const scope = 'alexa:all';
    const scopeData = {
        [scope]: {
          productID: deviceId,
          productInstanceAttributes: {
            deviceSerialNumber: deviceSerialNumber
          }
        }
      };
const authUrl = 'https://www.amazon.com/ap/oa?client_id=' + clientId + '&scope=' + encodeURIComponent(scope) + '&scope_data=' + encodeURIComponent(JSON.stringify(scopeData)) + '&response_type=' + responseType + '&redirect_uri=' + encodeURI(redirectUri);
        console.log("abhi avs.js promptUserLogin newWindow");
        urlopen.open(authUrl);
  }


function getTokenFromCode(CODE) {

    return new Promise((resolve, reject) => {
      if (typeof CODE !== 'string') {
        const error = new TypeError('`code` must be a string.');
        this._log(error);
        return reject(error);
      }


      const grantType = 'authorization_code';
      var postData = 'grant_type=' + grantType + '&code=' + CODE + '&client_id=' + clientId + '&client_secret=' + clientSecret + '&redirect_uri=' + encodeURIComponent(redirectUri);

      const url = 'https://api.amazon.com/auth/o2/token';
      const xhr = new XMLHttpRequest();
      xhr.open('POST', url, true);
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
      xhr.onload = (event) => {
        let response = xhr.response;
        response = JSON.parse(xhr.response);

        const isObject = response instanceof Object;
        const errorDescription = isObject && response.error_description;
        if (errorDescription) {
          const error = new Error(errorDescription);
          console.log(error);
          return reject(error);
        }
        const token = response.access_token;
        const refreshToken = response.refresh_token;
        const tokenType = response.token_type;
        const expiresIn = response.expiresIn;

        //this.setToken(token)
        //this.setRefreshToken(refreshToken)

        //this.emit(AVS.EventTypes.LOGIN)
        console.log('abhi avs.js Logged in.');
        resolve(response);
      };
     xhr.onerror = (error) => {
        this._log(error);
        reject(error);
      };
      xhr.send(postData);
    });
  }

I ran it in terminal with command node test.js . once we run it , it will open amazon login page , we will be redirected to the URL with code in it. https://localhost:9745/authresponse?code=ANcUMLaDrkMtCwUSrIqc&scope=alexa%3Aall

Here code is ANcUMLaDrkMtCwUSrIqc. pass this string as a value and enter 1 to execute the next steps. then it will gave error as

undefined ^

SyntaxError: Unexpected token u at Object.parse (native) at xhr.onload (/home/saiabhi/Avs/avs_test.js:100:25) at dispatchEvent (/home/saiabhi/Avs/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:591:25) at setState (/home/saiabhi/Avs/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:614:14) at IncomingMessage. (/home/saiabhi/Avs/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:447:13) at emitNone (events.js:72:20) at IncomingMessage.emit (events.js:166:7) at endReadableNT (_stream_readable.js:913:12) at nextTickCallbackWith2Args (node.js:442:9) at process._tickCallback (node.js:356:17)

Ref link : https://miguelmota.com/blog/alexa-voice-service-authentication/
when i ran using curl command for same values, it worked perfectly.

curl -X POST --data "grant_type=${GRANT_TYPE}&code=${CODE}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&redirect_uri=${REDIRECT_URI}" https://api.amazon.com/auth/o2/token


what am i doing wrong. please let me know.
Abhishek
  • 71
  • 8
  • Break your code down to the smallest part that reproduces the problem. 99% likely you'll find the problem on your own doing that anyway. – Brad Oct 24 '16 at 21:49

1 Answers1

1

You code is not clean. I cant say is it works.. but first of all, I recommend change

 xhr.response

to

 xhr.responseText;

An when you call promise, I always recommend to finish it by chain catch, because you can lost errors

'use strict';

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var clientId = "amzn1.application-oa2-client.775a44579eaf461b92db3d1a4cb23a5a";
var deviceId = "Test_device1";
var deviceSerialNumber = 123;
var redirectUri = "https://localhost:9745/authresponse";
var Urlcode = "ANcUMLaDrkMtCwUSrIqc";
var clientSecret = "29a6520d97d11d640e030786e133ccec9ead67005aaa45c212e72e10b00900ff";

getTokenFromCode(Urlcode)
    .then(function(result){
        console.log(result);
    })
    .catch(function (error) {
        console.log(error);
    });


function getTokenFromCode(CODE) {

    return new Promise((resolve, reject) => {
        if (typeof CODE !== 'string') {
            const error = new TypeError('`code` must be a string.');
            this._log(error);
            return reject(error);
        }

        const grantType = 'authorization_code';
        var postData = 'grant_type=' + grantType + '&code=' + CODE + '&client_id=' + clientId + '&client_secret=' + clientSecret + '&redirect_uri=' + encodeURIComponent(redirectUri);

        const url = 'https://api.amazon.com/auth/o2/token';
        const xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
        xhr.onload = () => {
            let response = xhr.responseText;
            console.log(response);
            response = JSON.parse(response);
            const isObject = response instanceof Object;
            const errorDescription = isObject && response.error_description;
            if (errorDescription) {
                const error = new Error(errorDescription);
                console.log(error);
                return reject(error);
            }
            const token = response.access_token;
            const refreshToken = response.refresh_token;
            const tokenType = response.token_type;
            const expiresIn = response.expiresIn;
            this.setToken(token)
            this.setRefreshToken(refreshToken)

            this.emit(AVS.EventTypes.LOGIN)
            resolve(response);
        };
        xhr.onerror = (error) => {
            this._log(error);
            reject(error);
        };
        xhr.send(postData);
    });
}
Denis Lisitskiy
  • 1,285
  • 11
  • 15