Create bearer authorization header in OkHttp java

后端 未结 3 2011
温柔的废话
温柔的废话 2021-02-07 19:56

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

example:

Authorization: Bearer eyJ0eXA

3条回答
  •  后悔当初
    2021-02-07 20:52

    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();
    

提交回复
热议问题