小编典典

Twitter API更新限制错误403

java

我正在尝试使用twitter4j api从twitter api检索数据。经过一段时间的检索数据后,出现以下错误:

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

我得出的结论是,以上错误得出了Twitter API支持的调用限制。如何创建间隔以便等待,直到可能从Twitter提取数据?

例如,我的代码如下:

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

我的新代码包含一个if语句,该语句检查status.getRemaining()是否接近零,然后等待15分钟,这实际上是持续时间。但是我遇到了TwitterResponse
response = twitter.getFollowerIDs(l);的问题。我收到消息:

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

阅读 453

收藏
2020-11-26

共1个答案

小编典典

您可以在每次回复时致电getRateLimitStatus()以获得一个RateLimitStatusRateLimitStats.getRemaining()会告诉您该调用族可用的API请求的剩余数量,如果该数量达到零,则可以调用RateLimitStatus.getSecondsUntilReset(),并至少等待那么长时间再进行其他调用。

有关Twitter速率限制的信息可以在以下位置找到:https : //dev.twitter.com/docs/rate-
limiting/1.1

这是一个基本示例:

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

在您现在提供的代码中,您要对Twitter进行2次调用,showUser并且getUserTimeLine。您需要这两个电话后检查速率限制状态(包括UserResponseList扩展TwitterResponse,并有速度限制信息)。这些调用属于2个不同的资源族(usersstatuses),每个速率限制窗口(15分钟)都允许调用这两种方法180次。

2020-11-26