Tuesday, July 2, 2013

0 How to query for tweets using twitter4j

Twitter has killed their API 1.0 on June 11, 2013. Now you cannot do a search query without authentication. Twitter4j is an excellent library for java developers to implement twitter things. You can update status, get a timeline, query for tweets using this library. Here I am going to show you how to query using twitter4j library

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

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

        try {
            Query query = new Query("#Shahbag");
            QueryResult result;
            result = twitter.search(query);
            List 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());
        }

0 comments:

 

Mohammad Abul Khaer Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates