Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Bearer Tokens in https request

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

like image 269
Werner der Champ Avatar asked Nov 06 '16 20:11

Werner der Champ


People also ask

What are bearer tokens used for?

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.

How do you consume a bearer token?

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.

Why do we need bearer?

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.

Can I pass bearer token in URL?

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.


1 Answers

Add auth to header in this way

var options = {        
        hostname: '<name of site>',
        port: 443,
        path: '<path>',
        method: 'GET',
        headers:{
            Authorization: ' Bearer <Token>'            
       }
};
like image 149
Denis Lisitskiy Avatar answered Sep 21 '22 05:09

Denis Lisitskiy