Android Picasso library, How to add authentication headers?

前端 未结 5 1008
南方客
南方客 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:33

    See bryant1410's answer for a more up-to-date solution.


    Something like this does the job for setting an API-key header:

    public class CustomPicasso {
    
        private static Picasso sPicasso;
    
        private CustomPicasso() {
        }
    
        public static Picasso getImageLoader(final Context context) {
            if (sPicasso == null) {
                Picasso.Builder builder = new Picasso.Builder(context);
                builder.downloader(new CustomOkHttpDownloader());
                sPicasso = builder.build();
            }
            return sPicasso;
        }
    
        private static class CustomOkHttpDownloader extends OkHttpDownloader {
    
            @Override
            protected HttpURLConnection openConnection(final Uri uri) throws IOException {
                HttpURLConnection connection = super.openConnection(uri);
                connection.setRequestProperty(Constants.HEADER_X_API_KEY, "MY_API_KEY");
                return connection;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 16:34

    A simple way like this will keep default OkHttpClient timeout and cache configurations:

    private class MyOkHttpDownloader extends OkHttpDownloader {
    
        public MyOkHttpDownloader(final Context context) {
            super(context);
            getClient().interceptors().add(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request newRequest = chain.request().newBuilder()
                            .addHeader("X-TOKEN", "VAL")
                            .build();
                    return chain.proceed(newRequest);
                }
            });
        }
    }
    
    Picasso picasso = new Picasso.Builder(context).downloader(new MyOkHttpDownloader(context)).build();
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-11-27 16:40

    Since Picasso 2.5.0 OkHttpDownloader class has been changed, assuming you are using OkHttp3 (and so picasso2-okhttp3-downloader), so you have to do something like this:

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

    Source: https://github.com/square/picasso/issues/900

    0 讨论(0)
  • 2020-11-27 16:41

    It's working

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                               .authenticator(new Authenticator()
                               {
                                   @Override
                                   public Request authenticate(Route route, Response response) throws IOException
                                   {
                                       String credential =  Credentials.basic("username","password");
                                       return response.request().newBuilder()
                                               .header("Authorization", credential)
                                               .build();
                                   }
                               }).build();
    
                       Picasso picasso = new Picasso.Builder(OnDemandImageCaptureActivity.this)
                               .downloader(new OkHttp3Downloader(okHttpClient))
                               .build();
                            picasso.load("http://example.com/abc.jpeg").into(ivcamera);
    

    dependency:

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
    
    0 讨论(0)
提交回复
热议问题