NodeJS Request Bearer

Par*_*opa 5 http-post node.js npm

I can't seem to get a proper response using the request package with node. I have tried several different placements according to the documentation on npm but keep getting the same result.

"Invalid Access Token 401"

I've asked around the forums and they ensure me that the token I'm using is correct. Any help would be greatly appreciated!

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);
Run Code Online (Sandbox Code Playgroud)

Mic*_*ash 7

授权元素应该在 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'
  }
};
Run Code Online (Sandbox Code Playgroud)

请试一试。