hello world
hello world

Reputation: 807

Load Images by passing authentication token Picasso Xamarin

I am using the Picasso component to load my images from the url into an ImageView. The issue is that I need to pass an Authentication token in the headers along with the URL to make sure I have access to it.

How can I pass the Authentication token along with the URL using the Picasso component?

Upvotes: 1

Views: 1582

Answers (2)

Matthew
Matthew

Reputation: 5222

Just finished binding the library by Jake:
- Java: https://github.com/JakeWharton/picasso2-okhttp3-downloader
- C#: https://github.com/mattleibow/square-bindings

The NuGet is JakeWharton.Picasso2OkHttp3Downloader (there may be a delay for the package to propagte around the world):

> Install-Package JakeWharton.Picasso2OkHttp3Downloader 

This can be used in the same way from C#:

using Square.OkHttp3;
using Square.Picasso;
using JakeWharton.Picasso;

// create the client
var client = new OkHttpClient.Builder()
    .AddInterceptor(chain =>
    {
        var newRequest = chain.Request()
            .NewBuilder()
            .AddHeader("X-TOKEN", "VAL")
            .Build();
        return chain.Proceed(newRequest);
    })
    .Build();

// create the picasso handle
var picasso = new Picasso.Builder(context)
    .Downloader(new OkHttp3Downloader(client))
    .Build();

// use picasso!
picasso
    .Load(url)
    .Placeholder(Resource.Drawable.placeholder)
    .Error(Resource.Drawable.error)
    .CenterInside()
    .Into(holder.image);

Note: this library use OkHttp v3. If this is not desired for some reason (should not cause any issues), then you will have to make use of answer provided by Cheesebaron.

Upvotes: 5

Cheesebaron
Cheesebaron

Reputation: 24470

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.

Upvotes: 3

Related Questions