简体   繁体   中英

twitter4j configuration

I'm trying to configure twitter4j to stream tweets

  1. I got (consumer key/secret, access token & access secret) from twitter already
  2. I created a new java project and imported twiiter4j as a library

Now I want to know how to configure it ( http://twitter4j.org/en/configuration.html )

The first way :
Save a standard properties file named "twitter4j.properties". Place it to either the current directory, root of the classpath directory.

I'm using netbeans and want to know the file type I should choose when I create properties file and where exactly I have to place it?

Answer to your questions:

  1. Select a normal text file type, add the contents and rename it to twitter4j.properties
  2. You may place the file in the root folder of your project, or in any folder in the classpath . Just be sure the folder is in the classpath , thats what needs to be taken care of.

good day.

It's not a better way to use twitter4j.properties file in production for standalone application. because you need to guarantee that no one from users can not read your secret keys.

In this reason, better to use configuration factory class with hardcoded secret keys and make configuration on the flow.

Check out this example it is for normal Twitter object but same method works for TwitterStream as well. Basically, use ConfigurationBuilder Twitter 4j with Netbeans .

Refer following code:

//Configuration for twitter4j
        ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
  .setOAuthConsumerKey("*********************")
  .setOAuthConsumerSecret("******************************************")
  .setOAuthAccessToken("**************************************************")
  .setOAuthAccessTokenSecret("******************************************");

//Example for fetching public tweets 
//Sample which gives all tweets of Kim Kardarshian

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    int pageno = 1;
    String user = "@KimKardashian";
    List<Status> statuses = new ArrayList<Status>();

    while (true) {
      try {
        int size = statuses.size(); 
        Paging page = new Paging(pageno++, 100);
        statuses.addAll(twitter.getUserTimeline(user, page));
        if (statuses.size() == size)
          break;
      }
      catch(TwitterException e) {
        e.printStackTrace();
      }
    }

//sample which gives "n" (here 10) tweets of particular user
        try{
         Twitter twitter = TwitterFactory.getSingleton();
         Paging p = new Paging();
          p.setCount(10);
         statuses.addAll(twitter.getUserTimeline("user",p));
       }catch(Exception e)
      { e.printStackTrace(); }
        for (Status st:statuses) {
            System.out.println(st.getText());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM