Skip to content

Instantly share code, notes, and snippets.

@gansai
Created October 3, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gansai/345cfee301afc7ea1535 to your computer and use it in GitHub Desktop.
Save gansai/345cfee301afc7ea1535 to your computer and use it in GitHub Desktop.
Post a Tweet from Java code
import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class TweetMain {
public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("Key")
.setOAuthConsumerSecret("secret")
.setOAuthAccessToken("token")
.setOAuthAccessTokenSecret("tokensecret");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Status status;
try {
status = twitter.updateStatus("First Tweet from within Java code @java,@twitter, @t4j_news");
System.out.println("Successfully updated the status to [" + status.getText() + "].");
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment