Twitter 4j with Netbeans

后端 未结 2 1570
不知归路
不知归路 2021-01-27 15:19

First time poster, but I\'m really stuck. I\'m working on a little project and I\'m trying to send out a tweet using a netbeans project. I\'m using twitter4j and it seems like

2条回答
  •  孤独总比滥情好
    2021-01-27 15:33

    Could I suggest an alternative route..

    I have recently been messing around with twitter4j and I approached this slightly differently - I found a nice and easy way to authenticate the client using a ConfigurationBuilder object and passing this to the factory that is getting the instance of the Twitter object you need.

    package main;
    
    import twitter4j.Twitter;
    import twitter4j.TwitterFactory;
    import twitter4j.TwitterStream;
    import twitter4j.TwitterStreamFactory;
    import twitter4j.conf.ConfigurationBuilder;
    
    public class Base {
    
        protected Twitter twitter;
        //protected TwitterStream twitterStream;
        private ConfigurationBuilder configBuilder;
    
        public Base(){
    
            configBuilder = new ConfigurationBuilder();
            configBuilder.setDebugEnabled(true);        
            configBuilder.setOAuthConsumerKey("[consumer key here]");
            configBuilder.setOAuthConsumerSecret("[consumer secret key here]");
            configBuilder.setOAuthAccessToken("[OAuthAccessToken here]");
            configBuilder.setOAuthAccessTokenSecret("[secret OAuthAccessToken here]");
    
            //use the ConfigBuilder.build() method and pass the result to the TwitterFactory
            TwitterFactory tf = new TwitterFactory(configBuilder.build());
            //you can now get authenticated instance of Twitter object.
            twitter = tf.getInstance();
        }
    }
    

    You could then extend this class with sub classes that implement the functionality you require or just create the ConfigurationBuilder/TwitterFactory/Twitter objects elsewhere in your code.

    Below I have implemented a class that creates status' and can return the Status object that holds additional information such as createdAt() and the ID etc etc.

    package main;
    
    import twitter4j.Status;
    import twitter4j.TwitterException;
    
    public class StatusUpdater extends Base{   
    
        public StatusUpdater(){}
    
        public Status updateStatus(String statusToUpdate) throws TwitterException{
            Status status = twitter.updateStatus(statusToUpdate);
            System.out.println("statusToUpdate: " + status + ".");
            return status;
        }
    }
    

    Then you can use the following statement to create the status. This can be done from mbean/ejb/servlet etc.

         try {
                StatusUpdater statusUpdater = new StatusUpdater();
                String statusTextToSet = "test status";
                Status updatedStatus = statusUpdater.updateStatus(statusTextToSet);
                System.out.println("Created at: " + updatedStatus.getCreatedAt());
            } catch (TwitterException tex) {
                System.out.println(tex.getErrorMessage());
            }
    

    More info on the configuration process here

提交回复
热议问题