I'm trying to connect to an API using 'auth' in the options. Currently it looks like this:
var options = {
hostname: '<name of site>',
port: 443,
path: '<path>',
auth:'Bearer <Token>',
method: 'GET'
};
However, I get Status Code 403 if I execute the request. When I put the following URL in the browser, it works:
https://<Host Name+ Path>?authorization=Bearer%20<Token>
I've already tried to change auth into Authorization=Bearer <Token>
and Authorisation:Bearer <Token>
but it didn't changed anything.
I'm probably just setting up the authorisation part not correctly, but couldn't find any info how auth
works
Thanks in advance
The most common way of accessing OAuth 2.0 APIs is using a “Bearer Token”. This is a single string which acts as the authentication of the API request, sent in an HTTP “Authorization” header. The string is meaningless to clients using it, and may be of varying lengths.
Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). 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.
Long before bearer authorization, this header was used for Basic authentication. For interoperability, the use of these headers is governed by W3C norms, so even if you're reading and writing the header, you should follow them. Bearer distinguishes the type of Authorization you're using, so it's important.
Don't pass bearer tokens in page URLs: Bearer tokens SHOULD NOT be passed in page URLs (for example as query string parameters). Instead, bearer tokens SHOULD be passed in HTTP message headers or message bodies for which confidentiality measures are taken.
Add auth to header in this way
var options = {
hostname: '<name of site>',
port: 443,
path: '<path>',
method: 'GET',
headers:{
Authorization: ' Bearer <Token>'
}
};
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