** Since I first released this tutorial in 2009, it has received thousands of views and has hopefully helped some of you get started with building projects incorporating Twitter with Processing. In late 2010, Twitter changed the way that authorization works, so I’ve updated the tutorial to get it inline with the new Twitter API functionality.
Accessing information from the Twitter API with Processing is (reasonably) easy. A few people have sent me e-mails asking how it all works, so I thought I’d write a very quick tutorial to get everyone up on their feet.
We don’t need to know too much about how the Twitter API functions, because someone has put together a very useful Java library to do all of the dirty work for us. It’s called twitter4j, and you can download it here. We’ll be using this in the first step of the building section of this tutorial.
1. Authentication
Here’s the tricky part. In the olden days, Twitter used to be happy with us sending it a username and password pair for authentication. Now, all of the Twitter API functionality uses an oAuth system, which allows people to authenticate applications through a login at twitter.com (so that developers never see the user’s login information).
That’s pretty good from a security standpoint, but it makes our job a little bit harder. Now, instead of just needing a short username and password, we need FOUR things to authenticate our sketch: A consumer key & secret, and an access token & secret. You can get all 4 of these things from the twitter developer page.
1. Visit https://dev.twitter.com/ and login with your twitter username and password
2. Click on the ‘Create an app’ button at the bottom right
3. Fill out the form that follows – you can use temporary values (like “Jer’s awesome test application”) for the first three fields.
4. Once you’ve agreed to the developer terms, you’ll arrive at a page which shows the first two of the four things that we need: the Consumer key and the Consumer secret. Copy and paste these somewhere so you have them ready to access.
5. To get the other two values that we need, click on the button that says ‘Recreate my access token’. Copy and paste those two values (Access token and Access token secret) so that we have all four in the same place.
2. Building
1. Open up a new Processing sketch.
2. Import the twitter4j library. In your downloaded twitter4j files, you’ll find the core .jar in a directory called ‘lib’. You’re looking for a file called something like ‘twitter4j-core-2.2.5.jar’. We can add this to our sketch by simply dragging the twitter4j-core-2.2.5.jar file onto your sketch window. If you want to check, you should now see this file in your sketch folder, inside of a new ‘code’ directory.
3. The first thing we’ll do in our new file is to store the credentials that we gathered in the first part of this tutorial into a ConfigurationBuilder object (this is a built-in part of the twitter4j library):
** Don’t use the credentials below – these are fake ones that I made up – use the ones that you copy & pasted in steps 4 & 5 above
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
4. Now we’ll make the main Twitter object that we can use to do pretty much anything you can do on the twitter website – get status updates, run search queries, find follower information, etc. This Twitter object gets built by something called the TwitterFactory, which needs our configuration information that we set above:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
5. Now that we have a Twitter object, we want to build a query to search via the Twitter API for a specific term or phrase. This is code that will not always work – sometimes the Twitter API might be down, or our search might not return any results, or we might not be connected to the internet. The Twitter object in twitter4j handles those types of conditions by throwing back an exception to us; we need to have a try/catch structure ready to deal with that if it happens:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#OWS");
try {
QueryResult result = twitter.search(query);
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
5. This code is working – but we haven’t done anything with the results. Here, we’ll set the results per page parameter for the query to 100 to get the last 100 results with the term ‘#OWS’ and spit those results into the output panel:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#OWS");
query.setRpp(100);
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
};
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
It seems stupid to come all of this way without doing something visual, so let’s throw all of the words that we see in the tweets onto the screen, quick and dirty, at random positions. The following code takes our existing instructions and puts them into the standard Processing setup/draw enclosures so that we can do some animation over time:
//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList<String> words = new ArrayList();
void setup() {
//Set the size of the stage, and the background to black.
size(550,550);
background(0);
smooth();
//Credentials
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
//Make the twitter object and prepare the query
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#OWS");
query.setRpp(100);
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
//Break the tweet into words
String[] input = msg.split(" ");
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
};
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
void draw() {
//Draw a faint black rectangle over what is currently on the stage so it fades over time.
fill(0,1);
rect(0,0,width,height);
//Draw a word from the list of words that we've built
int i = (frameCount % words.size());
String word = words.get(i);
//Put it somewhere random on the stage, with a random size and colour
fill(255,random(50,150));
textSize(random(10,30));
text(word, random(width), random(height));
}
** Extra-coolness: see this example working in the browser with Processing.js!!
That’s it! This example is very simple, but it’s the bare bones of what you need build a project which connects Twitter through Processing.
Shortly I’ll add another post which shows how to use Processing with the Twitter Streaming API, so that we can deal with large volumes of tweets over time.
Good luck!














29 Comments
Great update Jer! Also good choice of hashtag!
Will this work on Processing 1.2.1?
thank u sir! u are awesome!
How do i write a code to get a users location?
Any help will be much appriciated!
Thanks
Just a quick word of caution; with the latest version of Twitter4J you might see errors related to "Google App Engine". To solve this go to the library and delete the directory twitter4j-appengine and twitter4j-appengine-2.2.4.jar.
See here for more details:
http://groups.google.com/group/twitter4j/browse_t…
I keep getting an unexpected token error on the line that starts with "cb.setOAuthConsumerKey" I cant get past step 1… running processing 1.2.1 and using twitter4j-2.2.5
any help/suggestions appreciated
worked like a charm, thanks for this!
Thanks a lot!!
Very helpfull !!! IT REALLY WORKS!
I just dl'ed the twitter4j zip from Github, but there's no .jar file. Is this the right url?
https://github.com/yusuke/twitter4j/downloads
The .zip file from the download link above had no .jar files. I was redirected to:
http://twitter4j.org/en/index.html#download
which contained them.
first of all, thanks a lot for this great tutorial, it was and still is really helpful. But anyhow I have a few questions:
1st: is it possible to receive tweets only from a particular user? If so, how could I solve this?
2nd: is it by any chance possible to filter the outcome by one language and without particular symbols?
… I would appreciate any helpful support, thanks in advance.
cheers
how do you query tweets that contain an exact phrase, where if i use "i will be there". I want to query for the tweets that contains the phrase "i will be there". The results can contain anything else as well, but it has to at least contain those exact words in that order.
Thanks for your example, it is really helpful!! Recently I am working on a project about extracting twitter feed and display it in another visual form with Kinect camera tracking! Your tutorial give me a great help. thanks
Use html entities to envelop your request in quotes – so for example it would be "i will be there"
this is the best instruction on how make a link between processing and twitter. Awesome!
Great example how would i display the whole tweet instead of just single words from it?
Hi! Thank you so much for this! I will use in my architecture project, if I manage to change some things.
Is it possible to set up the script to show specific words (lets say words that begin with the letter "T") in a color different from the rest? I have no background in this area and I just watched your lecture on TED and was very inspired, i tried this, and managed to make it work and now I am trying to work out creating random colors for the words as well (maybe this would then lead to what I want for the end product)
Thank you so much again! your work is amazing and inspiring!
How can I display the tweets on the screen without breaking them up into words?
ArrayList words = new ArrayList();
void setup() {
That gives me an error: unexpected token void
Its somehow connected to the ArrayList, can anyone explain me this?
I’m already stuck at the first few lines
Thanks in advance.
Hi, great tutorial, thank you . I did create a sketch thanks to it, now I'm wiling to get it on line, trough processing's, but i'm kind of lost. How does this works with processingjs? Is processing-twitter.js any way related/dependent to/of twitter4j? For me, i do not know javaScript, where is a good place to go to learn how to use processing-twitter.js?
thanks in advance
vk
If your code is as above, it is missing the closing brackets to setup method. This should compile (untested), but does nothing more than initialize an ArrayList; Those are Processing/java code. May help to look at Processig.org at learning tab.
ArrayList words = new ArrayList();
void setup() {}
Hey I dragged the twitter4j-core-3.0.1.jar file to my sketch and tried to run some code but it could not find the class or type named "Tweet."
I am not sure what I am doing wrong or how to fix this? Suggestions?
Hey I dragged the twitter4j-core-3.0.1.jar file to my sketch and tried to run some code but it could not find the class or type named "Tweet."
Hi,
I am very new to processing and API's.. I entered the credentials and code but received an error, The function query.setRpp(100); does not exist, would anyone know how to fix this?
Any response is greatly appreciated.
Grace
i have succeeded using twitter API, however, i do not know how to collect tweets from specific time(which i purposely setup)
i used
query.setSince("2013-01-01")
but the result still gives me tweets from today..
can anyone fix this?
also, does anyone know the limit of amout of tweets i can get from API?
i have read API limiting ..but i am still confused. ..
is the limit based on only the amount? or the time? or both?
can i still collect tweets from past?
help plz
I tried this and got an error: "Cannot find a type or class named 'Date.'"
I was able to fix it by adding one line: import java.util.*;
Apparently newer versions of Processing only use "official" Java libraries. In any case, after adding this the tutorial works like a charm!
Also, it's been awhile since this tutorial was written, so Twitter4j has gone through a few versions. I fiddled with the recent versions of Twitter4j and found them pretty complicated (it's huge). If you just want the .jar file listed above, you can find it here: http://grepcode.com/snapshot/repo1.maven.org/mave…
I dragged & dropped that file onto the Processing sketch window and it worked fine.
Thank you so much !
I just had to do some research on the new version of twitter4j (did this on the 3.0.3 with Processing 2.0b.7). Some classes or method changed a bit, for those I had to deal with in this tutorial (but I'm sure many others had too) I found that:
- Tweet became Status.
- Rpp() became count().
- getFromUser() became getUser() so you need to add a line which get the name of you user with getName() like this:
User u=(User) t.getUser();
String user=u.getName();
I hope I'm not reposting, and I hope this helped someone because I'm a noob and it's my very first help comment.
Thanks again, and excuse my english i'm french.
H.
Agreed. The new version of Twitter4j changes a bunch of stuff. Here is the middle part of the code showing the new necessary changes. (Also had to add 'Import java.util.Date;' at the very top to get the Date line to work)
//Make the twitter object and prepare the query
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("art");
query.setCount(100);
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Status t = (Status)tweets.get(i);
User user = t.getUser();
String name = user.getName();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + name + " at " + d + ": " + msg);
3 Trackbacks
[...] Processing & Twitter [...]
[...] http://blog.blprnt.com/blog/blprnt/updated-quick-tutorial-processing-twitter [...]
[...] Nick had us work with a hack I had tried previously from Jer @ blprnt (http://blog.blprnt.com/blog/blprnt/updated-quick-tutorial-processing-twitter). This one needs a user defined API from dev.twitter.com to generate “tokens” that [...]