Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the (OAuth token) Authorization Header on an Android OKHTTPClient request

I'm able to set the Auth Header on normal HTTPURLConnection requests like this:

URL url = new URL(source);  
HttpURLConnection connection = this.client.open(url);  
connection.setRequestMethod("GET");  
connection.setRequestProperty("Authorization", "Bearer " + token);  

This is standard for HttpURLConnection. In the above code snippet this.client is an instance of Square's OkHTTPClient (here).

I'm wondering if there is an OkHTTP-specific way of setting the Auth Header? I see the OkAuthenticator class but am not clear on how exactly to use it / it looks like it only handles authentication challenges.

Thanks in advance for any pointers.

like image 564
Alfie Hanssen Avatar asked Jul 09 '13 19:07

Alfie Hanssen


People also ask

How do I pass the Authorization header in GET request?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

How do I send the Authorization header in HTTP?

It is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send.

How do I send Authorization header bearer?

To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.

How to implement OAuth2 (Bearer Token) Authentication in your app?

In this tutorial we are going to learn how to implement OAuth2 (bearer Token) authentication in your app using the retrofit. Please add Retrofit and OkHttp dependencies in you build.gradle file. To add your Access Token in every authenticated request you need to add an authentication intercepter in your OkHttp client.

How to extract access token from okhttp request header?

Now you just need to create a request interceptor (OAuthIntercepter) which extends Interceptor class of OkHttp library. Then, override intercept function and add your Access Token into the request header. Extract Access Token and Use it with Authorization header. Let’s code it.

What is a clients OAuth request?

Clients will direct a user’s browser to the authorization server to begin the OAuth process. Clients may use either the authorization code grant type or the implicit grant. Along with the type of grant specified by the response_type parameter, the request will have a number of other parameters to indicate the specifics of the request.

What is an access token and how does it work?

The idea is that the access token is added as an Authorization HTTP header on requests to let the API know we have access to a particular resource. OkHttp provides Interceptors which can alter web requests before they are sent out and Authenticators that allow us to re-sign and retry requests that have failed due to authorization.


1 Answers

If you use the current version (2.0.0), you can add a header to a request:

Request request = new Request.Builder()
            .url("https://api.yourapi...")
            .header("ApiKey", "xxxxxxxx")
            .build();

Instead of using:

connection.setRequestMethod("GET");    
connection.setRequestProperty("ApiKey", "xxxxxxxx");

However, for the older versions (1.x), I think the implementation you use is the only way to achieve that. As their changelog mentions:

Version 2.0.0-RC1 2014-05-23

New Request and Response types, each with their own builder. There's also a RequestBody class to write the request body to the network and a ResponseBody to read the response body from the network. The standalone Headers class offers full access to the HTTP headers.

like image 54
pt2121 Avatar answered Oct 14 '22 05:10

pt2121