Android Picasso library, How to add authentication headers?

前端 未结 5 1103
南方客
南方客 2020-11-27 16:08

I have tried setting a custom OkHttpClient with a custom Authenticator, however as the doc says: \"Responds to authentication challenges from the remote web or proxy server.

5条回答
  •  北海茫月
    2020-11-27 16:36

    You can also add Authentication as suggested in the documentation of OkHttp

    Just add this client

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

    to Picasso like this:

    final Picasso picasso = new Picasso.Builder(this)
                    .downloader(new OkHttp3Downloader(client))
                    .build();
    Picasso.setSingletonInstance(picasso);
    

    The only dependency needed is:

    compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'
    

提交回复
热议问题