How to send a tweet using twitter4j in java?

I can log in to twitter using twitter4j and get information about the registered user, but I can not post a tweet from my application. I am using a java application to publish tweets. see the code below that I am using.

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!");
        twitter.updateStatus(tweet);
    } catch (TwitterException e) {
        System.err.println("Error occurred while updating the status!");
    }

I get the following exception:

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

Please, help.

+5
source share
3 answers
  • Go to the already created application (https://dev.twitter.com)

  • Check application settings (> Application Type)

  • " " ", ". , .

( )

+7

, / . .

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

       ConfigurationBuilder cb = new ConfigurationBuilder();
       cb.setDebugEnabled(true)
           .setOAuthConsumerKey(consumerKey)
           .setOAuthConsumerSecret(consumerSecret)
           .setOAuthAccessToken(accessToken)
           .setOAuthAccessTokenSecret(accessSecret);

       try 
       {
          TwitterFactory factory = new TwitterFactory(cb.build());
          Twitter twitter = factory.getInstance();

          System.out.println(twitter.getScreenName());
          Status status = twitter.updateStatus(latestStatus);
          System.out.println("Successfully updated the status to [" + status.getText() + "].");
           }catch (TwitterException te) {
              te.printStackTrace();
              System.exit(-1);
           }
+4

Status code 401 means that you are not authorized

Check if your credentials are valid. If so, your application may not have been verified by the user. See examples on twitter4j (paragraph 7)

+1
source

All Articles