Login to Foursquare with an Android App using OAuth

Note: This tutorial is for the Foursquare API V1. A version for v2 can be found here.

Although the Foursquare login process is pretty easy, it took me some time to figure out how this could be done from an Android application. The problem here is, that I don’t want a Popup window for the OAuth login. There’s a different requirement for the whole login process.

A standard OAuth login process consists of this flow:

  • Create consumer with application token and secret
  • Get login URL from service
  • Redirect the user to the login URL
  • User grant or deny access to service
  • Get the access token and secret

But in a mobile application, without a URL redirection, what we actually want is something like this:

  • Create consumer with application token and secret
  • Login to service
  • Get the access token and secret

OK, first we need an OAuth library. In this example I’m going to user the Signpost lib. You can get it from here: http://code.google.com/p/oauth-signpost/

There are 3 available libraries, but for this example we need only the core and the commonshttp4 part. Then, it’s actually pretty easy to login. We can to this in just one step with the user Email-address and his password.

// Build URL and create GET request
String url = "http://api.foursquare.com/v1/authexchange" +
				"?fs_username=" +
				EMAIL +
				"&fs_password=" +
				PW;
HttpGet reqLogin = new HttpGet(url);

// Instantiate consumer with application key and secret
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(
		CONSUMER_KEY,
		CONSUMER_SECRET);
consumer.sign(reqLogin);

// Login
HttpClient httpClient = new DefaultHttpClient();
HttpResponse resLogin = httpClient.execute(reqLogin);
if (resLogin.getEntity() == null) {
	throw new Exception("Could not login");
}

// Parse response
Document document =
	DocumentBuilderFactory
		.newInstance()
			.newDocumentBuilder()
				.parse(resLogin.getEntity().getContent());

// Get access token
Element eOAuthToken =
	(Element)document.getElementsByTagName("oauth_token").item(0);
Node e = eOAuthToken.getFirstChild();
String sOAuthToken =  e.getNodeValue();
System.out.println("token: " +sOAuthToken);

// Get access secret
Element eOAuthTokenSecret =
	(Element)document.getElementsByTagName("oauth_token_secret").item(0);
e = eOAuthTokenSecret.getFirstChild();
String sOAuthTokenSecret =  e.getNodeValue();
System.out.println("Secret: " +sOAuthTokenSecret);

// Set access token and secret for further requests
consumer.setTokenWithSecret(sOAuthToken, sOAuthTokenSecret);

// Get user information
HttpGet requestVenue = new HttpGet("http://api.foursquare.com/v1/user");
consumer.sign(requestVenue);
HttpResponse resVenue = httpClient.execute(requestVenue);
if (resVenue.getEntity() == null) {
	throw new Exception("Could not get user information");
}

InputStream is = resVenue.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null; 

while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
}

System.out.println(sb.toString());

Of course, this login process has to be done just once. We just need to store the access token and the secret somewhere for further use.

That’s it. Happy coding!

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.