简体   繁体   English

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

[英]twitter4j: 401 authentication credentials missing or incorrect

I'm currently trying to get the userTimeline of a bunch of users.我目前正在尝试获取一堆用户的 userTimeline。 This has worked for me in the past, but now doesn't work anymore.这在过去对我有用,但现在不再有效。 I'm using twitter4j and using oauth.我正在使用 twitter4j 和 oauth。 I've registered 2 applications on my account so far, which both should be able to access the twitter-api.到目前为止,我已经在我的帐户上注册了 2 个应用程序,它们都应该能够访问 twitter-api。 However since yesterday I'm getting a 401 - authentication redentials missing or incorrect (tried both applications).但是,从昨天开始,我收到了 401 - 身份验证redentials 丢失或不正确(尝试了两个应用程序)。 Did twitter make any recent changes to the API? twitter 最近是否对 API 进行了任何更改? Should I setup a new twitter account?我应该设置一个新的 twitter 帐户吗? 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

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.问题是当我在 dev.twitter.com 上注册我的应用程序时,我没有设置回调 URL。 Once i set that, everything just worked.一旦我设置好了,一切都正常了。

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.登录到https://dev.twitter.com/并转到我的应用程序意味着您尊敬的应用程序。 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.并运行该程序并确保您的 lib 文件夹中有所有 jar 文件。 and problem will be solved.问题将得到解决。

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.打开Twitter认证window。 Enter proper credentials and sign in.输入正确的凭据并登录。

It worked.有效。 So then I tried:所以我尝试了:

Open the Twitter authentication window.打开Twitter认证window。 Close the Twitter authentication window.关闭Twitter认证window。 Open the Twitter authentication window again.再次打开Twitter认证window。 Enter proper credentials and hit Sign in.输入正确的凭据并点击登录。

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. 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.简而言之,确保客户端使用的身份验证 URL 与服务器期望的身份匹配。

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

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_token Once it is set - you will see Twitter Authorize screen. 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.您也可以按“Test OAuth”使用 OAuth 工具对其进行测试,并通过https://api.twitter.com/oauth/request_token作为 POST 请求的 Request URI

I faced the same issue and I fixed, If the device date and time is not correct then this problem will occur..我遇到了同样的问题并修复了,如果设备日期和时间不正确,则会出现此问题..

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.sbt file您可以使用以下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