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.
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);
}
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)
Twitter Official API has the bother limitation of time constraints, you can't get older tweets than a week.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With