java 如何在java中使用twitter4j发布推文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10006016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:13:58  来源:igfitidea点击:

how to post tweet using twitter4j in java?

javatwitter4j

提问by CodeNotFound

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.

我可以使用 twitter4j 登录 Twitter 并获取有关登录用户的信息,但我无法从我的应用程序发布推文。我正在使用 Java Web 应用程序发布推文。请参阅下面我使用的代码。

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 follwing exception:

我得到以下异常:

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

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

Please help.

请帮忙。

回答by 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.

  1. 转到您已经创建的应用程序 (https://dev.twitter.com)

  2. 检查您的应用程序设置(> 应用程序类型)

  3. “应用程序类型”需要是读取、写入和访问直接消息之一。如果没有,您可以轻松进行更新。

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

(我在处理环境中测试了您的代码并且它正在工作)

回答by vikiiii

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

回答by Boosty

Statuscode 401 means you're not authorized

状态码 401 表示您 未被授权

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)

检查您的凭据是否有效。如果是,则可能您的应用程序尚未得到用户的验证。请参阅twitter4j 站点上的示例(第 7 点)