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.



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:

‘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();

	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(""
				+ form.getGeolat() + "&geolong=" + form.getGeolong()
				+ "&shout=" + shout + "&venue=" + venueName);
	HttpURLConnection request = (HttpURLConnection) url.openConnection();

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

	// send the request

	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 from java standard API>

3. HttpURLConnection connection is an instance of 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) {

	return response.toString();


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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s