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)
I used this tutorial to search for tweets using twitter api 1.1 with OAuth Authentication
I have modified the code for my usability and it does not use twitter4j, which is good at the moment because OAuth search is not available in the RC build (I read it somewhere unable to find the location at the moment)
Also added getting twitter timeline
Code is in Groovy
TweetsInfo
is a Pojo class withString text, String fromUser, String expandedURL, String createdAt
(this was my requirement)Hope this helps :)
You can use the codebird js library for tweet search. All you need is to create an app on Twitter and note down the following:
Download codebird js Library from the GitHub repository here:
Usage:
The problem is that you are setting the consumer secret and token multiple times, as indicated by the exception:
It's happening because
TwitterFactory.getInstance()
is returning a singleton ofTwitter
, this is then havingsetOAuthConsumer
andsetOAuthAccessToken
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 ofTwitter
by usingTwitterFactory.getInstance(AccessToken)
:An added benefit of this factory method is that it may return a cached, authenticated, instance of
Twitter
for you.