我似乎无法使用带有 node.js 的请求包获得正确的响应。我根据 npm 上的文档尝试了几种不同的位置,但一直得到相同的结果。
“无效的访问 token
401"
我在论坛上问过,他们确保我使用的 token 是正确的。任何帮助将不胜感激!
const restify = require('restify');
const request = require('request');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log('%s listening to %s', server.name, server.url);
});
var options = {
url: 'https://css.api.hp.com/productWarranty/v1/queries',
json: true,
method: 'POST',
Authorization: 'Bearer MYAUTHORIZATIONKEY',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/plain',
}
};
var callback = (error, response, body) => {
console.log(body);
console.log(response.statusCode);
}
request(options, callback);
最佳答案
授权元素应该在 headers 对象中。像这样:
var options = {
url: '<your url>',
method: 'POST',
json: requestBody,
headers: {
'User-Agent': 'my request',
'Authorization': 'Bearer <your token>',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
请试一试。
关于node.js - NodeJS 请求承载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531566/