how to post tweet using twitter4j in java?

Go To


I am able to login to twitter using twitter4j and get information about the logged in user but i am not able to post tweet from my application. I am using java web application to post tweet. see the code below that i use.

String ACCESS_TOKEN = "ttttttttt";
    String ACCESS_TOKEN_SECRET = "gggggggggggggg";
    String CONSUMER_KEY = "gggggggggggg";
    String CONSUMER_SECRET = "hhhhhhhhhhhhh";
    String tweet = "";
    if (request.getParameter("tweet") != null) {
        tweet = request.getParameter("tweet");
    AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
    OAuthAuthorization authorization = new OAuthAuthorization(ConfigurationContext.getInstance(), CONSUMER_KEY, CONSUMER_SECRET, accessToken);
    Twitter twitter = new TwitterFactory().getInstance(authorization);
    try {
        //twitter.updateStatus("Hello World!");
    } catch (TwitterException e) {
        System.err.println("Error occurred while updating the status!");

i get the follwing exception:

TwitterException{exceptionCode=[fa54b184-3bf2623f], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.5-SNAPSHOT(build: d372a51b9b419cbd73d416474f4a855f3e889507)}

Please help.

2012-04-04 06:34
by SAR


  1. Go to your already created application (

  2. Check your application settings (> Application Type)

  3. 'Application Type' need to be one of Read, Write and Access direct messages. If not you can easily make an update.

(I tested your code in processing environment and it is working)

2012-05-23 17:39
by vag


First of all , regenerate your acces token/secret and consumer key/secret. Then try this.

String consumerKey = "yourconsumerKey ";
       String consumerSecret = "yourconsumerSecret";
       String accessToken = "yourAccessToken";
       String accessSecret = "yourAccessSecret";

       ConfigurationBuilder cb = new ConfigurationBuilder();

          TwitterFactory factory = new TwitterFactory(;
          Twitter twitter = factory.getInstance();

          Status status = twitter.updateStatus(latestStatus);
          System.out.println("Successfully updated the status to [" + status.getText() + "].");
           }catch (TwitterException te) {
2012-04-04 07:20
by vikiiii


Statuscode 401 means you're not authorized

Check if your credentials are valid. If they are, maybe your application has not been validated by the user. See the examples on twitter4j site (point 7)

2012-04-04 07:04
by Glenn VdB