7

I am using picasso 2.5.2 library to download bitmap from remote server, the image url requires basic authentication in header.

i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries.

Answer - 1

Answer - 2

Answer - 3

enter image description here

Thanks in advance.

darwin
  • 1,524
  • 1
  • 22
  • 32
  • Please add your code - how do you want to authenticate? Have you looked into the [Retrofit docs](https://futurestud.io/blog/android-basic-authentication-with-retrofit). `Retrofit2.Builder()` has a property called `authorization`, which is probably what you are looking for. – yennsarah Apr 22 '16 at 06:24
  • 1
    I am not looking for adding basic authentication with retrofit rest api calls, i need to add basic authentication with picasso for downloading images. – darwin Apr 22 '16 at 06:30
  • If you looked like 5 seconds in the provided link, you would have found a way to configure a `okhttp3` client modification. – yennsarah Apr 22 '16 at 06:34

1 Answers1

14

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

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .authenticator(new Authenticator()
                {
                    @Override
                    public Request authenticate(Route route, Response response) throws IOException
                    {
                        String credential = Credentials.basic("user", "pass");
                        return response.request().newBuilder()
                                .header("Authorization", credential)
                                .build();
                    }
                })
            .build();

Then, use that client in forming your Picasso object, but with okhttp3 you will have to use a OkHttp3Downloader instead, like so:

    Picasso picasso = new Picasso.Builder(context)
        .downloader(new OkHttp3Downloader(okHttpClient))
        .build();

You can get the OkHttp3Downloader from https://github.com/JakeWharton/picasso2-okhttp3-downloader

Flavius
  • 447
  • 3
  • 9