Twitter API更新限制错误403 [英] Twitter API update limits error 403

查看:198
本文介绍了Twitter API更新限制错误403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用twitter4j api从twitter api中检索数据。检索数据一段时间后,我收到以下错误:

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

我得出结论,上述错误导致了调用的限制twitter api的支持。如何才能创建间隔才能等到可以从twitter获取数据?

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?

例如我的代码如下:

            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)的问题;我收到消息:

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


推荐答案

在每个回复中,您可以调用 getRateLimitStatus()来获取a RateLimitStatus RateLimitStats.getRemaining()将告诉您可用于该系列调用的剩余API请求数,如果达到零,则可以调用 RateLimitStatus。 getSecondsUntilReset(),并且在拨打额外电话之前至少等待很长时间。

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.

有关Twitters速率限制的信息,请点击此处: https://dev.twitter.com/docs/rate-limiting/1.1

Information on Twitters rate limits can be found here: 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);

在您现在提供的代码中,您正在拨打2个来电, showUser getUserTimeLine 。在这两个调用之后你需要检查速率限制状态(用户 ResponseList extend TwitterResponse 并有速率限制信息)。这些调用属于2个不同的资源系列(用户状态),这两种方法都被允许调用180每个速率限制窗口的时间(15分钟)。

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

这篇关于Twitter API更新限制错误403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆