簡體   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