Simple OAuth using SignPost

SignPost is dead simple java API for OAuth by Matthias Kappler. I would like to share with everyone how I used this to connect to Foursquare.

Establishing a Connection to Foursquare

Establishing a connection to foursquare require us to create a provider and retrieve a request token using a valid consumer key. so the first thing we need to do is to create a Provider object.

OAuthProvider provider = new DefaultOAuthProvider( FOURSQUARE_OAUTH_REQUEST_TOKEN, FOURSQUARE_OAUTH_ACCESS_TOKEN,FOURSQUARE_OAUTH_AUTHORIZE);

where:
1. FOURSQUARE_OAUTH_REQUEST_TOKEN is “http://foursquare.com/oauth/request_token”.
2. FOURSQUARE_OAUTH_ACCESS_TOKEN is “http://foursquare.com/oauth/access_token”.
3. FOURSQUARE_OAUTH_AUTHORIZE is “”http://foursquare.com/oauth/authorize”.

Next we need to create a consumer object.

private OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);

We need to obtain CONSUMER_KEY and CONSUMER_SECRET from Foursquare OAuth registration page. You need to provide foursquare your application name, application URL, and a call back URL.

Once we have obtained the consumer key and consumer secret we are ready to connect to foursquare. Connecting to foursquare Oauth is verys simple:

String authURL = provider.retrieveRequestToken(consumer,OAuth.OUT_OF_BAND);

authURL is a link to Foursquare authorization web page where user need to give their consent for our application to access their data from foursquare. I will refer this to authorization url from this point onward

Foursquare Authentication

Our application need to redirect user to foursquare authorization url. This what four square authorization page looks like.

Foursquare will send our user to the call back URL that we provided to Foursquare if use decided to grant access rights to your application. Foursqure will append a random string that acts a “PIN Code” that we can use to obtain an access token.

This is an example of a pin code:

http://locate-for-me.appspot.com/authCallBack.do?oauth_token=R128TYJAGSFGAIUASDKFHLASDHF918237E76IAIK

‘R128TYJAGSFGAIUASDKFHLASDHF918237E76IAIK’ is a pin code that we will use to request for access token.

Requesting Access Token

We will use the provider and consumer object that we have constructed to request for an access token. We can use this access token to retrieve tips, checking, add shouts, etc to foursquare.

public AccessToken requestAccessToken(String pinCode) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {

        provider.retrieveAccessToken(consumer, pinCode);
	String token = consumer.getToken();
	String tokenSecret = consumer.getTokenSecret();

	AccessToken accessToken = new AccessToken();
	accessToken.setToken(token);
	accessToken.setTokenSecret(tokenSecret);
	accessToken.setPinCode(pinCode);

	return accessToken;

}

Everything is done !!! Now you are ready to use the Access Token to check in to foursquare.

Checking In to FourSquare

This is an example how to check in to foursquare.

public String checkin(AccessToken accessToken, FoursquareForm form) throws IOException, OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException {

	String venueName = form.getVenueName();
	String shout = URLEncoder.encode(form.getShout(), "UTF-16");

	// create an HTTP request to foursquare check in
	URL url = new URL("http://api.foursquare.com/v1/checkin.json?geolat="
				+ form.getGeolat() + "&geolong=" + form.getGeolong()
				+ "&shout=" + shout + "&venue=" + venueName);
	HttpURLConnection request = (HttpURLConnection) url.openConnection();
	request.setRequestMethod("POST");

	// sign the request
	consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());
	consumer.sign(request);

	// send the request
	request.connect();

	return readRequest(request.getInputStream());
}

1. FoursquareForm is a custom class that I wrote to encapsulate data that user submit from the web. The form contain:

  • Geolat: the user latitude location.
  • Geolong: the user longitude location.
  • Shout: user shout.
  • Venue Name: the name of the venue that our user wants to check-in to.

2. URL url is an instance of java.net.URL from java standard API>

3. HttpURLConnection connection is an instance of java.net.HttpURLConnection from java standard API.

4. The readRequest method is a simple method to read the response that Foursquare sends back to our application. The response comes in a form of JSON that we can parse in webpage.

private String readRequest(InputStream inputStream) throws IOException {
	BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));

	String inputLine;
	StringBuffer response = new StringBuffer();

	while ((inputLine = in.readLine()) != null) {
		response.append(inputLine);
	}
	in.close();

	return response.toString();

}
Advertisements

About zainul franciscus

Hi, I am a software developer and tech blogger who is currently living in Wellington, New Zealand. I write stuff that I find interesting on
This entry was posted in java, locate-for-me, programming, social media, web development and tagged , , . Bookmark the permalink.

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