How to search for tweets using twitter API 1.1 and java

MAK Ripon picture MAK Ripon · Jun 13, 2013 · Viewed 21.3k times · Source

Twitter API has been changed from 1.0 to 1.1. Now for any type of query it has to be authorized. I am using java for fetching tweets. Can anyone give me some java example of tweet fetching using OAuth authentication.

Update

Using twitter4j api it is possible. http://twitter4j.org/en/. An example is given below

Twitter twitter = new TwitterFactory().getInstance();

    AccessToken accessToken = new AccessToken("Your-Access-Token", "Your-Access-Token-Secret");
    twitter.setOAuthConsumer("Consumer-Key", "Consumer-Key-Secret");
    twitter.setOAuthAccessToken(accessToken);

    try {
        Query query = new Query("#IPL");
        QueryResult result;
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
        }
    }
    catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }

Problems here

This example works independently when I ran as a Java class. But when I add this code in a JSP for testing in webapp it does not work. It shows me following exception

    SEVERE: Servlet.service() for servlet [jsp] in context with path [/mypub] threw exception [java.lang.IllegalStateException: consumer key/secret pair already set.] with root cause
    java.lang.IllegalStateException: consumer key/secret pair already set.
        at twitter4j.TwitterBaseImpl.setOAuthConsumer(TwitterBaseImpl.java:264)
        at com.me.framework.tag.core.TweetFetch.doTag(TweetFetch.java:50)
        at org.apache.jsp.template.test_jsp._jspx_meth_wf_002dcore_005ftweetFetch_005f0(test_jsp.java:100)
        at org.apache.jsp.template.test_jsp._jspService(test_jsp.java:74)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

Answer

Jonathan picture Jonathan · Jun 17, 2013

The problem is that you are setting the consumer secret and token multiple times, as indicated by the exception:

java.lang.IllegalStateException: consumer key/secret pair already set.

It's happening because TwitterFactory.getInstance() is returning a singleton of Twitter, this is then having setOAuthConsumer and setOAuthAccessToken invoked on it each time a request is made to your Servlet.

You need to ensure you only configure your Twitter instance once and not each time a request is made.

One way of achieving this is by asking the TwitterFactory to give you an authenticated instance of Twitter by using TwitterFactory.getInstance(AccessToken):

final AccessToken accessToken = new AccessToken("Your-Access-Token", "Your-Access-Token-Secret");
final Twitter twitter = TwitterFactory.getInstance(token);
...

An added benefit of this factory method is that it may return a cached, authenticated, instance of Twitter for you.