Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a java application to read twitter feeds?

Tags:

java

twitter

I want to create a small application that will access twitter api to read feeds using application level authentication only. I have read many docs over net and feeling extremely confused. I understand that twitter api needs OAth to authorize any application to get or write data from/into twitter. For getting the keys associated(consumer secret key) the dev apps page of twitter asks us to create a new app which I created to get the keys. Now I have some tutorials that say how to make a properties file to save those keys and start running the java application.

The problem is even after following everything i am not able to run the application. Can anyone describe in a step by step method about creating a java application to read feeds, setting up all the configurations needed, creating and explaining all the steps needed to get the keys ? I am using twitter 4j.

like image 260
Aditya Raman Avatar asked Oct 19 '22 21:10

Aditya Raman


1 Answers

Its so simple to create a java client to read twitter messages using some third party libraries like Twitter4j etc. You did not mention which tweets you wanted to read whether your own tweets or someone else's, anyway I followed this blog and got my work done :)

  1. Crate a Twitter application on https://apps.twitter.com/app/new and get your consumer key. (Follow the mentioned blog if you struck somewhere.)
  2. Using the Twitter4j API get the authorization URL, hit it and get the PIN. (The code required is mentioned below for your ref)
  3. Input the pin and get access tocken (refer below code)
  4. That's all now we can read or update twitter feeds. (Based on the access level you set while creating twitter application)

Sample code:

 Twitter twitter = new TwitterFactory().getInstance();
 twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);

RequestToken requestToken = twitter.getOAuthRequestToken();
 System.out.println("Authorization URL: \n"
  + requestToken.getAuthorizationURL());

AccessToken accessToken = null;

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

   try{
       System.out.print("Hit above Authorization URL and Input PIN here: ");
       String pin = br.readLine();

       accessToken = twitter.getOAuthAccessToken(requestToken, pin);

     } catch (TwitterException te) {

        System.out.println("Failed to get access token, caused by: "
           + te.getMessage());
     }

 System.out.println("Access Token: " + accessToken.getToken());
 System.out.println("Access Token Secret: "
  + accessToken.getTokenSecret());

 // updating twitter status
 twitter.updateStatus("hi.. im updating this using Namex Tweet for Demo");

System.out.println("\nReading Twitter Timeline:");

 // I'm reading your timeline
 ResponseList list = twitter.getHomeTimeline();
 for (Status each : list) {

     System.out.println("Sent by: @" + each.getUser().getScreenName()
      + " - " + each.getUser().getName() + "\n" + each.getText()
      + "\n");
 }

Check it and comment here if you face any issues.

like image 155
Mohan Kanakala Avatar answered Oct 22 '22 09:10

Mohan Kanakala