Access the Twitter REST API (v1.1) from Scala and Java using signpost

3 minute read

If you’ve read some other articles on this blog you might know that I like creating visualizations of various datasets. I’ve just started a small project where I want to visualize some data from Twitter. For this I want to retrieve information about followers and profile information directly from twitter. I actually started looking for a set of all twitter accounts, but could only find one that was two years old. So, only option left, directly access the twitter API and get the data myself.

There are a couple of open source libraries out that we can use directly from Scala (or java) but as far as I could see they use the old v1 API and not the v1.1 API. The old API has a very strict data rate limit, which is a bit lighter in the new API. And besides that I’m more interested in the raw data and parsing the returning JSON isn’t that hard with Scala (or Java for that matter).

Register an application at twitter

First thing to do, and easiest way to get started is registing a new application for your twitter account. Go to and create a new application. Don’t worry about the urls, since we won’t be using the OAuth callback mechanism:

Create 1 | Twitter Developers.png

Depending on what you want to do with the API you need to give additional permissions to this app. Default is “read-only”, if you want to allow your new application to post or access your direct messages you need to update the permissions. This is done from the settings page of your application:

SmartjavaTest | Twitter Developers.png

Once you’ve created the application and setup the correct permissions you can generate an access token. Doing this will avoid having to go through the complete OAuth dance. You do this by going to your new app details and at the bottom select the “create my access token” option.

Create 2 | Twitter Developers.png

Now you’ll have a set of tokens (see the details part of your applications):

SmartjavaTest | Twitter Developers-1.png

We’ll use these tokens to authenticate the requests we’ll make to twitter.

Using a OAuth library

The OAuth protocol is a pretty good documented protocol, but implementing it yourself is a lot of work and very error-prone. Luckily there are many OAuth libraries that can help you. I’ve tried a couple and the one that’s most easy to use (at least for me) was signpost. The examples below show how to do this from Scala, but you can follow the same approach for Java.

First the dependencies. I’ve used sbt and from signpost I use the client with support for HTTP commons. In sbt add the following:

libraryDependencies ++= Seq(
	"oauth.signpost" % "signpost-core" % "1.2",
	"oauth.signpost" % "signpost-commonshttp4" % "1.2", 
	"org.apache.httpcomponents" % "httpclient" % "4.2",

For maven you can use the same libraries. Next we can write a simple test to see if everything is working. In java it looks like this:

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Tw {
	  static String AccessToken = "access token for your app";
	  static String AccessSecret = "access secret for your app";
	  static String ConsumerKey = "consumer key for your app";
	  static String ConsumerSecret = "consumer secret for your app";
	 * @param args
	public static void main(String[] args) throws Exception {
		OAuthConsumer consumer = new CommonsHttpOAuthConsumer(

        consumer.setTokenWithSecret(AccessToken, AccessSecret);
        HttpGet request = new HttpGet("");

        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(request);

        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode + ":" + response.getStatusLine().getReasonPhrase());

And in Scala it looks pretty much the same:

import org.apache.http.client.HttpClient
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpGet
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer

object TwitterPull {

	  val AccessToken = "access token for your app";
	  val AccessSecret = "access secret for your app";
	  val ConsumerKey = "consumer key for your app";
	  val ConsumerSecret = "consumer secret for your app";
  def main(args: Array[String]) {
	 val consumer = new CommonsHttpOAuthConsumer(ConsumerKey,ConsumerSecret);
	 consumer.setTokenWithSecret(AccessToken, AccessSecret);
     val request = new HttpGet("");
     val client = new DefaultHttpClient();
     val response = client.execute(request);

When you run this the output will look something like this:


If you get a 403 check whether the tokens match.