Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Could I suggest an alternative route..</p> <p>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.</p> <pre><code>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(); } } </code></pre> <p>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.</p> <p>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.</p> <pre><code>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; } } </code></pre> <p>Then you can use the following statement to create the status. This can be done from mbean/ejb/servlet etc. </p> <pre><code> 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()); } </code></pre> <p>More info on the configuration process <a href="http://twitter4j.org/en/configuration.html" rel="nofollow">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload