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

发布于 2024-11-04 09:36:19 字数 2764 浏览 0 评论 0原文

我目前正在尝试获取一群用户的 userTimeline 。这过去对我有用,但现在不再有用了。我正在使用 twitter4j 并使用 oauth。到目前为止,我已经在我的帐户上注册了 2 个应用程序,它们都应该能够访问 twitter-api。然而,从昨天开始,我收到了 401 - 身份验证凭据丢失或不正确(尝试了两个应用程序)。 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

I'm currently trying to get the userTimeline of a bunch of users. This has worked for me in the past, but now doesn't work anymore. I'm using twitter4j and using oauth. I've registered 2 applications on my account so far, which both should be able to access the twitter-api. However since yesterday I'm getting a 401 - authentication redentials missing or incorrect (tried both applications). Did twitter make any recent changes to the API? Should I setup a new twitter account? Is it an appropriate way to create an application and allow it to access your own account?

This is the code:

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

  }

the error:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无法言说的痛 2024-11-11 09:36:19

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

I had the same issue. The consumer key and consumer secret I was using were correct and my machine was in sync. The problem was that i did not set the callback URL when i was registering my application on dev.twitter.com. Once i set that, everything just worked.

◇流星雨 2024-11-11 09:36:19

Consumer key 和 Token 有问题。请按照步骤操作。

登录 https://dev.twitter.com/ 并转到“我的应用程序”意味着您尊敬的应用程序。
并转到设置和更改访问权限
只读

读取、写入和直接消息

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

There is problem with Consumer key and Token. follow the steps.

login to https://dev.twitter.com/ and Goto My application means your respected application.
and goto settings and Change access permission
Read-only
to
Read, write, and direct messages

and update data and if it is not updated then recreate token and update data and your program too. and run the program and make sure you are having all jar file of that in your lib folder.
and problem will be solved.

沙沙粒小 2024-11-11 09:36:19

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

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

打开 Twitter 身份验证窗口。
输入正确的凭据并登录。

它成功了。于是我尝试:

打开 Twitter 身份验证窗口。
关闭 Twitter 身份验证窗口。
再次打开 Twitter 身份验证窗口。
输入正确的凭据并点击“登录”。

后者总是会导致 401 身份验证凭据丢失或不正确的异常。

我的问题是服务器正在重新启动 Twitter 身份验证过程,但 RequestToken.getAuthenticationURL() 的值未正确传递回 UI,并导致它使用旧 URL 调用 Twitter API。

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

I thought I would share this in case it helps someone Googling for this exception message.

While testing Twitter authentication for a JSF application, I tried the following use case:

Open the Twitter authentication window.
Enter proper credentials and sign in.

It worked. So then I tried:

Open the Twitter authentication window.
Close the Twitter authentication window.
Open the Twitter authentication window again.
Enter proper credentials and hit Sign in.

The latter always resulted in a 401 authentication credentials missing or incorrect exception.

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.

In brief, ensure the authentication URL used by the client matches the one the server is expecting.

〆凄凉。 2024-11-11 09:36:19

今天 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>
  ...

Today 02.05.2015 I got the similar error using a twitter4j 3.0.x version.

I got it until I updated twitter4j to version 4.0.0.

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

笨死的猪 2024-11-11 09:36:19

我也遇到了这个问题,并使用了正确的系统时间和 API 密钥/秘密。

事实证明,需要将回调 URL 设置为 https://api.twitter.com/oauth /request_token
设置完成后 - 您将看到 Twitter 授权屏幕。

您还可以按“测试 OAuth”使用 OAuth 工具进行测试并传递 https://api. twitter.com/oauth/request_token 作为 POST 请求的请求 URI。

I faced this issue as well with correct system time and 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.

You can also press "Test OAuth" to test it using OAuth Tool and pass https://api.twitter.com/oauth/request_token as a Request URI for POST request.

原来分手还会想你 2024-11-11 09:36:19

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

I faced the same issue and I fixed, If the device date and time is not correct then this problem will occur..

故乡的云 2024-11-11 09:36:19

检查您的所有身份验证凭据是否正确
a) 消费者密钥
b) 消费者秘密
c) 访问令牌
d) 访问令牌秘密
如果以上所有内容都正确,它应该可以工作。

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

您可以使用下面的 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"
)

Check all your auth credentials are correct
a) consumerKey
b) consumerSecret
c) accessToken
d) accessTokenSecret
If all the above are correct, It should work.

Another possibility can be when your system is out of sync. Such as if you are running in virtual box after few hours of inactivity your system get out of sync. Please restart the system and try again.

You can validate the dependencies with below build.sbt file

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"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文