I tried to use this Android Picasso library, How to add authentication headers? to access a protected image that returns the base64 version of the image. My problem is that the picasso always failed. and I don't know why. the authorization code is valid since the profile details are loaded. only the image was not. Here is my implementation how to get the image.
public class PicaAuth {
private static Picasso sPicasso;
private PicaAuth() {
}
public static Picasso getImageLoader(final Context context) {
if (sPicasso == null) {
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new CustomOkHttpDownloader(context));
sPicasso = builder.build();
}
return sPicasso;
}
private static class CustomOkHttpDownloader extends OkHttpDownloader {
public CustomOkHttpDownloader(Context context) {
super(context);
}
@Override
protected HttpURLConnection openConnection(final Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty("Authorization", Auth.getBearerAccessToken());
return connection;
}
}
}
Main Activity
PicaAuth.getImageLoader(MainActivity.this)
.load(uri)
.into(mImage, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
Log.d("Image Success");
}
@Override
public void onError() {
Log.e("Image Failed");
}
});