我正在尝试使用 xmlhttprequest 向 URL 发送请求并使用 Node js 运行 js 文件。但对它的反应是不确定的。当我使用 CURL 命令发布相同的数据时,我得到了正确的响应。为什么它在 nodejs 和 xmlhttprequest 的情况下不起作用。
这是我写的代码。文件名为 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);
});
}
我使用命令 node test.js 在终端中运行它。一旦我们运行它,它将打开亚马逊登录页面,我们将被重定向到其中包含代码的 URL。 https://localhost:9745/authresponse?code=ANcUMLaDrkMtCwUSrIqc&scope=alexa%3Aall
这里的代码是 ANcUMLaDrkMtCwUSrIqc。将此字符串作为值传递并输入 1 以执行后续步骤。那么它会给出错误
未定义 ^
SyntaxError: 在 dispatchEvent (/home/saiabhi/Avs/node_modules/xmlhttprequest/lib/XMLHttpRequest. js:591:25) 在 IncomingMessage 的 setState (/home/saiabhi/Avs/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:614:14)。(/home/saiabhi/Avs/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:447:13) 在emitNone (events.js:72:20) 在 IncomingMessage.emit (events.js:166:7) 在 endReadableNT (_stream_readable .js:913:12) 在 nextTickCallbackWith2Args (node.js:442:9) 在 process._tickCallback (node.js:356:17)
参考链接:https
://miguelmota.com/blog/alexa-voice-service-authentication/
当我使用 curl 命令运行相同的值时,它运行良好。
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.