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.
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.
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.
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.
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.
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.
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.
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.
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.
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