Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create bearer authorization header in OkHttp java

Tags:

java

okhttp3

I need to use OkHttp3 in java as a HTTP client and send Authorization header in request.

example:

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRaswczovL2F1dGgucGF4aW11bS5djb20iLCJhdWQiOiJodHRwczovL2FwaS5wYXhpbXVtLmNvbSIsIm5iZiI6MTQ0ODQzNzkyMCwiZXhwIjoxNDQ4NDgxMTIwLCJzdWIiOiIzNzExZDk1YS03MWU1LTRjM2ItOWQ1YS03ZmY3MGI0NDgwYWMiLCJyb2xlIjoicGF4OmIyYjphcHA6dXNlciJ9.YR8Gs7RVM-q5AxtHpeOl2zYe-zKxh5u39TUeTbiZL1k

how can I create this token using my username and password? username: test password: test

like image 353
Amir Hossein Khalouei Avatar asked Mar 24 '18 10:03

Amir Hossein Khalouei


People also ask

How do I pass Authorization bearer in header?

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 do I send a POST request with Bearer Token Authorization header Java?

To send a POST JSON request with a Bearer Token authorization header, you need to make an HTTP POST request, provide your Bearer Token with an Authorization: Bearer {token} HTTP header and give the JSON data in the body of the POST message.

What is bearer Authorization header?

The bearer token is a cryptic string, usually generated by the server in response to a login request. The client must send this token in the Authorization header when making requests to protected resources: Authorization: Bearer.

How do I send an Authorization header 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.


Video Answer


2 Answers

According to the documentation here

  private final OkHttpClient client = new OkHttpClient();
  private final String url = "http://test.com";

  public void run(String token) throws Exception {
    Request request = new Request.Builder()
    .url(url)
    //This adds the token to the header.
    .addHeader("Authorization", "Bearer " + token)
    .build();
     try (Response response = client.newCall(request).execute()) {
          if (!response.isSuccessful()){
             throw new IOException("Unexpected code " + response);
          }

         System.out.println("Server: " + response.header("anykey"));

     }
  }
like image 126
Thankgod Richard Avatar answered Sep 21 '22 06:09

Thankgod Richard


The above answer lead to correct path but need some changes.

private  Response requestBuilderWithBearerToken(String userToken) throws IOException {
           OkHttpClient client = new OkHttpClient();
           Request request = new Request.Builder()
                   .url(YourURL)
                   .get()
                   .addHeader("cache-control", "no-cache")
                   .addHeader("Authorization" , "Bearer " + userToken)
                   .build();

           return  client.newCall(request).execute();
like image 41
user1154390 Avatar answered Sep 22 '22 06:09

user1154390