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);
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With