質問

I am trying to retrieve data from twitter api with the twitter4j api. After some time retrievig data I am getting the following error:

Exception in thread "main" 403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following).
message - User has been suspended.
code - 63

I came to conclusion that the above error derive limits of the calls that the twitter api support. How is it possible to create intervals in order to wait until it could be possible to fetched data from twitter?

For example my code is the following:

            Long l = Long.parseLong(list.get(index));
            TwitterResponse response = twitter.getFollowersIDs(l);
            RateLimitStatus status = response.getRateLimitStatus();
            if (status.getRemaining() < 2) {
                try {
                    System.out.println("status.getSecondsUntilReset()");
                    Thread.sleep(status.getSecondsUntilReset());
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
            User user = twitter.showUser((l));
            //statuses = twitter.getUserTimeline(l);
            JSONObject features = new JSONObject();

            features.put("_id", l);
            score = kloutScore(l);

My new code contains a if statement which check if status.getRemaining() is close to zero then it waits for 15 minutes which is actually the limit duration. However I got problems with TwitterResponse response = twitter.getFollowerIDs(l); I am getting the message:

Exception in thread "main" 429:Returned in API v1.1 when a request cannot be served due to the application's rate limit having been exhausted for the resource. See Rate Limiting in API v1.1.(https://dev.twitter.com/docs/rate-limiting/1.1)
message - Rate limit exceeded
役に立ちましたか?

解決

On each response you can call getRateLimitStatus() to get a RateLimitStatus. RateLimitStats.getRemaining() will tell you the remaining number of API requests available for that family of calls, if that reaches zero you could then call RateLimitStatus.getSecondsUntilReset(), and wait at least that long before making additional calls.

Information on Twitters rate limits can be found here: https://dev.twitter.com/docs/rate-limiting/1.1

Here is a basic example:

do {
    TwitterResponse response = twitter.getFollowersIDs(userId, cursor);
    RateLimitStatus status = response.getRateLimitStatus();
    if(status.getRemaining() == 0) {
        try {
            Thread.sleep(status.getSecondsUntilReset() * 1000);
        }
        catch(InterruptedException e) {
            // ...
        }
    }
} while(cursor > 0);

In the code you have now provided you are making 2 calls to Twitter, showUser and getUserTimeLine. You need to check the rate limit status after both of these calls (both User and ResponseList extend TwitterResponse and have the rate limit information). These calls belong to 2 different resource families (users and statuses), both of these methods are allowed to be called 180 times per rate limit window (15 minutes).

他のヒント

I had this same error and upgrading to the latest version of twitter4j 3.0.5 fixed my issue. Get the latest here http://twitter4j.org/en/index.html#download I should mention that the version i was running was twitter4j 3.0.3. So by going to version 3.0.5 or 3.0.6 fixed the my issue.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top