Android Picasso Library, How to Add Authentication Headers

Android Picasso library, How to add authentication headers?

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

How to add authentication token in header in Picasso library

It took two days to resolve this problem. For custom downloader you don't have to call with method because this will initialize the default downloader & picasso instance. Simply do below like this that will help you to get bitmap.

Picasso.Builder builder = new Picasso.Builder(getActivity());
picasso = builder.downloader(new OkHttpDownloader(getActivity()) {
@Override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty(Constant.HEADER_X_API_KEY, mSharedPreferences.getString(SharedPreferenceKeys.JSESSIONID, ""));
return connection;
}
}).build();
picasso.load(url).into(mTarget);

Android Picasso, add custom HTTP header in version 2.5.2

Based on Android Picasso library, How to add authentication headers?

I've solved in this way

dependencies:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.5.0'

Code

 OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("X-CUSTOM-HEADER", "my-header-value")
.build();
return chain.proceed(newRequest);
}
});

return new Picasso.Builder(context).downloader(new OkHttpDownloader(okHttpClient)).build();

Thanks for help

How to add Basic Authentication in Picasso 2.5.2 with OkHttp 3.2.0

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

How to add header to Android Picasso

You need to add Interceptor to the OkHttpClient you are providing to the constructor of OkHttpDownloader.

Also you need to use this OkHttp3Downloader instead, which is support for okhttp3. Probably, you might want to use a snapshot version of the picasso where OkHttp3Downloader is probably included in the library.

Reader more about Interceptors here

Load Images by passing authentication token Picasso Xamarin

The Picasso library on NuGet and the Xamarin Component store is super old. It hasn't been updated in over a year. Hence there might be slight differences from the code you see out there from what you have available.

If you need to add a header to your image requests you can implement your own IDownloader which you hand to Picasso:

public class CustomDownloader : OkHttpDownloader
{
public CustomDownloader(IntPtr handle, JniHandleOwnership transfer)
: base(handle, transfer)
{ }

public CustomDownloader(string authtoken, Context context) : base(context)
{
Client.Interceptors().Add(new MyInterceptor(authtoken));
}

public class MyInterceptor : Java.Lang.Object, IInterceptor
{
private string _authtoken;

public MyInterceptor(string authtoken)
{
_authtoken = authtoken;
}

public Response Intercept(IInterceptorChain chain)
{
var newRequest = chain.Request().NewBuilder().AddHeader("Authentication", _authtoken).Build();
return chain.Proceed(newRequest);
}
}
}

You can then add this custom downloader like:

var token = "authtoken";
var builder = new Picasso.Builder(this).Downloader(new CustomDownloader(token, this)).Build();

Then as usual you can download your image into an ImageView as usual with:

builder.Load(Android.Net.Uri.Parse("https://test.com/img.jpg")).Into(imageView);

I've tested this against Requestb.in and the Authentication header is set just fine.

You can obviously set any header you want.



Related Topics



Leave a reply



Submit