Authenticating With Twitter

In the previous section, you should have downloaded the necessary software and created a Twitter ‘application’ on the Twitter website. If not, access the previous part of this tutorial here.

In this section we will begin writing some code and authenticate ourselves with Twitter.

1. Getting Started

Let’s begin by creating a simple empty Processing window. Add the following code to your sketch:

void setup()
{
    size(800,600);
}

void draw()
{

}

Run the program and you should now have an empty window that is 800 by 600 pixels in size.

2. Importing Libraries

We are going to need to import the Twitter4J library if we are to use it in Processing. Do this by adding the following to the very top of your sketch:

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;

We are also going to need to import the java.util package as we will make use of the List and Date classes when dealing with tweets we retrieve from Twitter. Add the following line just after the other import statements:

import java.util.*;

Now, run the program and check that the sketch still produces the same result as in step 1. If you have any problems you may need to check you have installed the Twitter4J library correctly.

3. Authenticating with Twitter

The first step in our Twitter application will be to create an instance of the `Twitter’ object. Directly after importing the libraries at the top of your sketch, add the following:

Twitter twitter;

We are going to use this later. But first, in order to be able to ask Twitter for information, we need to authenticate our application with the API application keys. There are four of these:

  • The “Consumer Key”
  • The “Consumer Secret”
  • The “Access Token”
  • The “Access Token Secret”

You should have created these earlier in the tutorial – if not go back and do that now as you will need them.

To authenticate ourselves with Twitter we are going to create a ConfigurationBuilder object and initialise it with our application keys. In the setup() function, create a new ConfigurationBuilder instance as follows:

ConfigurationBuilder cb = new ConfigurationBuilder();

Directly after that, initialise the object with your application keys (I have replaced them with asterisks as I can’t publish keys here):

cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******");
cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");

Now we are going to create a TwitterFactory object and pass it our ConfigurationBuilder object:

TwitterFactory tf = new TwitterFactory(cb.build());

Finally, we will initialise our Twitter object by retrieving an instance from the `TwitterFactory’:

twitter = tf.getInstance();

4. Full Source So Far

Your full code should now look like the following:

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;

Twitter twitter;

void setup()
{
    size(800,600);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******");
    cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
    cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
    cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");

    TwitterFactory tf = new TwitterFactory(cb.build());

    twitter = tf.getInstance();
}

void draw()
{

}

Now run the program and check it produces no errors.

In the next section we will begin asking Twitter for information and displaying it using Processing.