how to post tweet using twitter4j in java?

让人想犯罪 __ 提交于 2019-12-04 09:11:27
vag
  1. Go to your already created application (https://dev.twitter.com)

  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)

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();
       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);
           }

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!