1

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?

SiHa
  • 7,830
  • 13
  • 34
  • 43
hello world
  • 797
  • 1
  • 9
  • 31
  • 1
    Possible duplicate of [Android Picasso library, How to add authentication headers?](http://stackoverflow.com/questions/24273783/android-picasso-library-how-to-add-authentication-headers) – Nikhil Sep 30 '16 at 10:56
  • ...and about url: `url += ("?or&token=" + token)` – Selvin Sep 30 '16 at 10:56
  • @indramurari I have gone through that one already. I wanted something equivalent in xamarin – hello world Sep 30 '16 at 10:58
  • @Selvin thanks for the help but it didnt work. Still gives me and unauthorized error – hello world Sep 30 '16 at 11:04

2 Answers2

5

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.

Community
  • 1
  • 1
Matthew
  • 4,832
  • 2
  • 29
  • 55
3

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.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • the component is a bit old, but the NuGet is up to date. I have just finished the binding of Jake's library and it will be up on NuGet soon. – Matthew Oct 03 '16 at 21:12
  • By default, Picasso uses OkHttp v2, and Jake's downloader uses v3. – Matthew Oct 03 '16 at 21:13