How to create a java app to read twitter feeds?

I want to create a small application that will access the twitter api to read feeds using only application level checking. I read a lot of documents over the network and am very confused. I understand that twitter api requires OAth to authorize any application to receive or write data from / to twitter. To get the associated keys (user private key), the dev apps page on twitter asks us to create a new application that I created to receive the keys. Now I have several tutorials that tell you how to make a properties file to save these keys and run a java application.

The problem is even after doing everything that I can not start the application. Can someone describe, step by step, a method of creating a Java application to read feeds, configure all the necessary configurations, create and explain all the steps necessary to get the keys? I use twitter 4j.

+3
source share
1 answer

It is so simple to create a java client for reading twitter messages using some third-party libraries such as Twitter4j, etc. You didnโ€™t indicate which tweets you wanted to read, whether itโ€™s your own tweets or someone elseโ€™s, in any case, I followed this blog and did my job :)

  • Attach the Twitter app to https://apps.twitter.com/app/new and get your consumer key. (Follow the indicated blog if you hit somewhere.)
  • Using the Twitter4j API, get the authorization URL, click it and get a PIN. (The required code is listed below for your ref)
  • Enter the output and get access to the window (see code below).
  • Now we can all read or update twitter feeds. (Based on the access level that you set when you create the Twitter application)

Code example:

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 out and comment here if you encounter any problems.

+2
source

Source: https://habr.com/ru/post/1499190/


All Articles