Twitter 4j with Netbeans

后端 未结 2 1569
不知归路
不知归路 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

    0 讨论(0)
  • 2021-01-27 15:46

    Your code looks ok. Have you confirmed your definitely using the correct consumer key and secret for your app? Try twitter.verifyCredentials() to see if you get the same error, or a more specific error.

    If you're correcting to the net via a proxy, you will need to include your proxy settings to allow the connection to succeed, details can be found here http://twitter4j.org/en/configuration.html#HTTP%20proxy%20server

    As an aside, you may want to remove you access token and secret from your post, if they're still valid then someone could login to your account with them.

    0 讨论(0)
提交回复
热议问题