Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Request Bearer

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);
like image 724
Parakoopa Avatar asked Apr 20 '17 23:04

Parakoopa


People also ask

What is bearer in node JS?

js applications. Bearer tokens are typically used protect API endpoints, and are often issued using OAuth 2.0. By plugging into Passport, bearer token support can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Is Bearer Token same as JWT?

JWT is a particular type of token. JWT can be used as an OAuth Bearer token. A useful resource for reference can be found at https://auth0.com/docs/tokens.

What is request bearer header?

A Bearer Token is a cryptic string typically generated by the server in response to a login request. The client must send this Bearer Token in the Authorization header on every request it makes to obtain a protected resource.

How do I add a Bearer Token to my header?

The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.


1 Answers

the authorization element should be in the headers object. Like this:

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'
  }
};

Please give that a try.

like image 117
Michael Bordash Avatar answered Nov 04 '22 23:11

Michael Bordash