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.
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 :)
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.
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