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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5868602/
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 13:12:36  来源:igfitidea点击:

twitter4j: 401 authentication credentials missing or incorrect

javatwittertwitter4j

提问by jcfrei

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?

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

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

回答by Punit Raizada

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.

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

回答by Krishna

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

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

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.

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

回答by Etienne Dufresne

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:

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

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

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

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.

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

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

后者总是导致 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.

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

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

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

回答by Dmitriy Sorochynskiy

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

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

I got it until I updated twitter4j to version 4.0.0.

直到我将 twitter4j 更新到 4.0.0 版才知道。

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

回答by Ivan V

I faced this issue as well with correct system time and API Key/Secret.

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

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

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

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

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

回答by Suresh

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

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

回答by adarsh

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

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

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.sbtfile

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