Skip to content

Instantly share code, notes, and snippets.

@patrick91
Created February 20, 2015 21:39
Show Gist options
  • Save patrick91/8195934407f720c6fcac to your computer and use it in GitHub Desktop.
Save patrick91/8195934407f720c6fcac to your computer and use it in GitHub Desktop.
Picasso Basic Auth
import android.util.Base64;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class BasicAuthInterceptor implements Interceptor {
String username;
String password;
public BasicAuthInterceptor(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public Response intercept(Chain chain) throws IOException {
String auth = Base64.encodeToString(username + ":" + password).getBytes(), Base64.NO_WRAP);
Request compressedRequest = chain.request().newBuilder()
.header("Authorization", "Basic " + auth)
.build();
return chain.proceed(compressedRequest);
}
}
public class MyApplication extends Application {
public Picasso getPicasso() {
Picasso.Builder builder = new Picasso.Builder(this);
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new BasicAuthInterceptor("user", "password"));
Downloader downloader = new OkHttpDownloader(client);
return builder.downloader(downloader).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment