【问题标题】:how to post tweet using twitter4j in java?如何在 Java 中使用 twitter4j 发布推文?
【发布时间】:2012-05-24 09:22:10
【问题描述】:

我可以使用 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!");
    }

我得到以下异常:

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

请帮忙。

【问题讨论】:

    标签: java twitter4j


    【解决方案1】:
    1. 转到您已创建的应用程序 (https://dev.twitter.com)

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

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

    (我在处理环境中测试了你的代码,它正在工作)

    【讨论】:

      【解决方案2】:

      首先,重新生成您的访问令牌/秘密和消费者密钥/秘密。 那就试试这个吧。

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

      【讨论】:

        【解决方案3】:

        状态码 401 表示您是 not authorized

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

        【讨论】:

          猜你喜欢
          • 2021-04-11
          • 2013-07-06
          • 1970-01-01
          • 2011-06-05
          • 1970-01-01
          • 1970-01-01
          • 2012-03-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多