微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 twitter4j 修复 Twitter v1 API 上的错误 401/32?

如何解决如何使用 twitter4j 修复 Twitter v1 API 上的错误 401/32?

我在使用 twitter4j 发推文时遇到问题,有时会抛出错误

401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret,access token/secret,and the system clock is in sync.
   message - Could not authenticate you.
   code - 32

有时我在使用它时根本没有收到错误并且推文发送正确,所以我相信这不是 api 密钥/秘密的错误

如果我尝试使用 twitter.verifyCredentials() 方法,则会正确返回用户。我在返回的用户中看不到任何错误信息。

这有点像我使用的java代码

public static twitter4j.Twitter getTwitter(
      String apiKey,String secret,String oauthToken,String oauthSecret) {

    final ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
        .setoAuthConsumerKey(apiKey)
        .setoAuthConsumerSecret(secret)
        .setoAuthAccesstoken(oauthToken)
        .setoAuthAccesstokenSecret(oauthSecret)
        .setTweetModeExtended(true);

    final TwitterFactory tf = new TwitterFactory(cb.build());
    twitter4j.Twitter t = tf.getInstance();
    return t;
  }

  public static void tweet(
      String apiKey,String oauthSecret,String message)
      throws TwitterException {

    twitter4j.Twitter twitter = getTwitter(apiKey,secret,oauthToken,oauthSecret);

    Status status = twitter.updateStatus(message);
  }

public static void main(String[] args) throws TwitterException {
            Keyword k = Keyword.load(1);
            String msg = "This is a test tweet";

         tweet(
            k.getTwitterapiKey(),k.getTwitterapiSecret(),k.getTwitterOAuthToken(),k.getTwitterOAuthSecret(),msg);
}

感谢任何帮助。如果我需要提供更多信息,请告诉我。

提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。