繁体   English   中英

twitter4j:401 身份验证凭据丢失或不正确

[英]twitter4j: 401 authentication credentials missing or incorrect

我目前正在尝试获取一堆用户的 userTimeline。 这在过去对我有用,但现在不再有效。 我正在使用 twitter4j 和 oauth。 到目前为止,我已经在我的帐户上注册了 2 个应用程序,它们都应该能够访问 twitter-api。 但是,从昨天开始,我收到了 401 - 身份验证redentials 丢失或不正确(尝试了两个应用程序)。 twitter 最近是否对 API 进行了任何更改? 我应该设置一个新的 twitter 帐户吗? 这是创建应用程序并允许它访问您自己的帐户的适当方式吗?

这是代码:

ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("q...w")
      .setOAuthConsumerSecret("R...o")
      .setOAuthAccessToken("2...7")
      .setOAuthAccessTokenSecret("O....8");

  TwitterFactory tf = new TwitterFactory(cb.build());

    Twitter twitter = tf.getInstance();
  for ( Map.Entry<Long, String> entry : tmls.entrySet()) {

        long uid = entry.getKey();

        Paging paging = new Paging(1, count);

        ResponseList<Status> rls;

        try {
            rls = twitter.getUserTimeline(uid, paging);

            for ( int j = 0 ; j < rls.size() ; j++ ) {

                if (false == ltindex.contains(uid)) {


                    alsa.add(new String[] {  String.valueOf( rls.get(j).getId() ) , String.valueOf( uid ), rls.get(j).getText() , "", entry.getValue(), String.valueOf( rls.get(j).getCreatedAt().getTime() )  });

                }

            }


        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

  }

错误:

  401:Authentication credentials (http://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
  error - Not authorized
  request - /1/statuses/user_timeline.json?user_id=14221532&include_rts=true&include_entities=false&count=10&page=1
  Relevant discussions can be on the Internet at:
    http://www.google.co.jp/search?q=ced778ef or
    http://www.google.co.jp/search?q=10a1ea9d
  TwitterException{exceptionCode=[ced778ef-10a1ea9d], statusCode=401, retryAfter=0, rateLimitStatus=RateLimitStatusJSONImpl{remainingHits=273, hourlyLimit=350, resetTimeInSeconds=1304422, secondsUntilReset=845, resetTime=Tue May 03 11:36:48 UTC 2011}, version=2.2.3-SNAPSHOT(build: ddf24547632bf3a28b899e3d75b110de43f71c0f)}
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189)
    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
    at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:85)
    at twitter4j.TwitterImpl.get(TwitterImpl.java:1738)
    at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:246)
    at getExpertTweets.getSpecificOpinions(getExpertTweets.java:89)
    at classifyTweet.main(classifyTweet.java:57)
  root@se1:~# sudo ntpdate pool.ntp.org

我遇到过同样的问题。 我使用的消费者密钥和消费者秘密是正确的,并且我的机器是同步的。 问题是当我在 dev.twitter.com 上注册我的应用程序时,我没有设置回调 URL。 一旦我设置好了,一切都正常了。

消费者密钥和令牌存在问题。 按照步骤。

登录到https://dev.twitter.com/并转到我的应用程序意味着您尊敬的应用程序。 并转到设置和更改访问权限只读以阅读、写入和直接消息

并更新数据,如果未更新,则重新创建令牌并更新数据和您的程序。 并运行该程序并确保您的 lib 文件夹中有所有 jar 文件。 问题将得到解决。

我想我会分享这个,以防它帮助某人在谷歌上搜索这个异常消息。

在测试 JSF 应用程序的 Twitter 身份验证时,我尝试了以下用例:

打开Twitter认证window。 输入正确的凭据并登录。

有效。 所以我尝试了:

打开Twitter认证window。 关闭Twitter认证window。 再次打开Twitter认证window。 输入正确的凭据并点击登录。

后者总是导致 401 身份验证凭据丢失或错误异常。

My problem was that the server was restarting the Twitter authentication process but the value of RequestToken.getAuthenticationURL() was not being properly passed back to the UI and caused it to invoke the Twitter API with the old URL.

简而言之,确保客户端使用的身份验证 URL 与服务器期望的身份匹配。

今天 02.05.2015 我使用 twitter4j 3.0.x 版本遇到了类似的错误。

在我将 twitter4j 更新到 4.0.0 版之前,我得到了它。

  <dependencies>
  <dependency>
      <groupId>org.twitter4j</groupId>
      <artifactId>twitter4j-core</artifactId>
      <version>[4.0,)</version>
  </dependency>
  ...

我在正确的系统时间和 API Key/Secret 下也遇到了这个问题。

It turned out that one needs to set Callback URL to https://api.twitter.com/oauth/request_token Once it is set - you will see Twitter Authorize screen.

您也可以按“Test OAuth”使用 OAuth 工具对其进行测试,并通过https://api.twitter.com/oauth/request_token作为 POST 请求的 Request URI

我遇到了同样的问题并修复了,如果设备日期和时间不正确,则会出现此问题..

检查您所有的身份验证凭据是否正确 a) consumerKey b) consumerSecret c) accessToken d) accessTokenSecret 如果以上所有内容都正确,它应该可以工作。

另一种可能性是当您的系统不同步时。 例如,如果您在几个小时不活动后在虚拟框中运行,您的系统就会失去同步。 请重新启动系统并重试。

您可以使用以下build.sbt文件验证依赖项

name := "sparkstreamingexamples"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-streaming" % "2.0.1",
  "org.apache.bahir" %% "spark-streaming-twitter" % "2.0.1",
"org.twitter4j" % "twitter4j-core" % "4.0.4"
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM