Android Picasso, How to add authentication headers?

Stevan
Stevan
Member
310 Points
20 Posts

I'm using Picasso library to download images from api endpoint:

 Picasso.get().load(imgUrl).into(holder.mImgProfileView);

Now I needed to download images from endpoint that is authorized. I need to pass authentication header to authenticate the endpoint.

Views: 4399
Total Answered: 1
Total Marked As Answer: 1
Posted On: 22-Feb-2020 05:09

Share:   fb twitter linkedin
Answers
Priya
Priya
Participant
936 Points
28 Posts
         

Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation:

OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder()
                        .addHeader("Authorization", "bearer <accesstoeken>")
                        .build();
                return chain.proceed(newRequest);
            }
        })
        .build();
Picasso picasso = new Picasso.Builder(context)
        .downloader(new OkHttp3Downloader(client))
        .build();

Now use it as:

picasso.load(imgUrl).into(holder.mImgProfileView);

 

Posted On: 20-Mar-2020 07:28
 Log In to Chat