Create bearer authorization header in OkHttp java

后端 未结 3 945
花落未央
花落未央 2021-02-07 20:29

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

example:

Authorization: Bearer eyJ0eXA

3条回答
  •  -上瘾入骨i
    2021-02-07 21:02

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

提交回复
热议问题