Android and basic authentication

I’ve been trying to play with the Github API in an Android app, but a couple wrenches in the machine made it a lot more difficult than anticipated. Every request I was making returned a 404. At first I thought the Android keystore didn’t have Github’s SSL cert and tried to create my own keystore. Eventually I figured out that it was basic authentication that wasn’t working correctly, and not SSL. If only Github’s API returned a 401 Unauthorized instead of a 404 Not found.

Anyway, many many hours later, I finally found this Stackoverflow answer which gave me the magic incantation for submitting an http request with basic authentication in the header:

String username = ...
String password = ...
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);

HttpRequest request = ...
request.addHeader(new BasicScheme().authenticate(creds, request));

I’m blogging it so I remember how to do it later.

Advertisements

2 thoughts on “Android and basic authentication

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s