Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for tweets using twitter API 1.1 and java

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)
like image 375
MAK Ripon Avatar asked Jun 13 '13 10:06

MAK Ripon


People also ask

Can you get old tweets from Twitter API?

Twitter Official API has the bother limitation of time constraints, you can't get older tweets than a week.

What is Twitter search API?

Search Tweets API is an HTTP-based RESTful API that returns responses encoded in JSON. All requests require a query parameter that contains filtering syntax for matching Tweets of interest. Query syntax is made up of operators that match on various Tweet and user attributes such as keywords, hashtags, URLs.


1 Answers

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.

like image 60
Jonathan Avatar answered Oct 02 '22 22:10

Jonathan