Java Twitter4j - 超出速率限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19999779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:11:40  来源:igfitidea点击:

Twitter4j - Rate limit exceeded

javatwittertwitter4j

提问by Matt

I would like to get followers using getFollowersIds()in Twitter4j, but I get

我想让关注者getFollowersIds()在 Twitter4j 中使用,但我得到

ConnectionErrorException ... rate limit exceeded

ConnectionErrorException ...超出速率限制

public static void main(String[] args) {
        try {
            Twitter twitter = TwitterFactory.getSingleton();
            String[] srch = new String[]{"TechCrunch"};
            ResponseList<User> users = twitter.lookupUsers(srch);
            for (User user : users) {

                UserHarvest us = new UserHarvest(6017542);
                us.getFollowersIds();
                try {
                    us.getContributors();
                } catch (ConnectionErrorException ex) {
                    Logger.getLogger(UserHarvest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (TwitterException ex) {
            Logger.getLogger(UserHarvest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Error message:

错误信息:

Exception in thread "main" harvest.twitterharvest.ConnectionErrorException: Connection could not have been established
    at harvest.twitterharvest.WrapperTwitter4J.getFollowersIDs(WrapperTwitter4J.java:75)
    at harvest.twitterharvest.UserHarvest.getFollowersIds(UserHarvest.java:106)
    at harvest.twitterharvest.UserHarvest.main(UserHarvest.java:140)
Caused by: 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
code - 88

Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=92c30ec6 or
    http://www.google.co.jp/search?q=19e1da11
TwitterException{exceptionCode=[92c30ec6-19e1da11], statusCode=429, message=Rate limit exceeded, code=88, retryAfter=-1, rateLimitStatus=RateLimitStatusJSONImpl{remaining=0, limit=15, resetTimeInSeconds=1384512813, secondsUntilReset=904}, version=3.0.4}
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:162)
    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
    at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:89)
    at twitter4j.TwitterImpl.get(TwitterImpl.java:1894)
    at twitter4j.TwitterImpl.getFollowersIDs(TwitterImpl.java:409)
    at harvest.twitterharvest.WrapperTwitter4J.getFollowersIDs(WrapperTwitter4J.java:73)
    ... 2 more

I see secondsUntilReset=904. I run code 1 hour later and get the same error message. I don't know why. Thanks for any help.

我明白了secondsUntilReset=904。我在 1 小时后运行代码并收到相同的错误消息。我不知道为什么。谢谢你的帮助。

采纳答案by air-dex

There are rate limits in the Twitter API. You cannot call a given Twitter API endpoint more than a given number of times per 15 minutes (on behalf of the authencated user or not).

Twitter API 中存在速率限制。您每 15 分钟调用给定的 Twitter API 端点的次数不能超过给定的次数(无论是否代表经过身份验证的用户)。

What happened to you is that your code must have reached the rate limit very quickly (the endpoint to retrieve followers IDsis limited to 15 calls per 15 minutes for a given authenticated user) so you will have to wait (904 seconds) before trying again.

发生在您身上的是您的代码必须很快达到速率限制(对于给定的经过身份验证的用户,检索关注者 ID 的端点限制为每 15 分钟 15 次调用),因此您必须等待(904 秒)才能再次尝试.

Be careful to the Twitter API endpoints you are calling (through Twitter4J) in order to economize your API calls and thus avoid reaching the rate limit.

请注意您(通过 Twitter4J)调用的 Twitter API 端点,以节省您的 API 调用,从而避免达到速率限制。

For further informations, have a look at those resources on Twitter Developers, especially at its documentation:

有关更多信息,请查看Twitter Developers上的这些资源,尤其是其文档

回答by mgokhanbakal

You dont have to wait to fix rate limit issue. If you have many tokens corresponding different twitter accounts, then you can renew your current used token, right. Think about the big picture. Let's say that you have 25 different accounts(tokens) in a list or array object, and you are getting one of them each time that you have a rate limit issue. Then when you reach to the last item in list token list, then you can refill this list with these 25 token accounts, right. The beauty is this, if you have good enough number of accounts(tokens), each refilling loop, the system automatically will be waited to recover the used accounts, so you will not have to worry about the rate limit and your application will never stop because of rate limit issue. I have already implemented this mechanism in my project and it works perfect. This is the only best solution.

您不必等待修复速率限制问题。如果你有很多代币对应不同的推特账号,那么你可以更新你当前使用的代币,对吧。想想大局。假设您在一个列表或数组对象中有 25 个不同的帐户(令牌),并且每次遇到速率限制问题时都会获得其中一个。然后当你到达列表令牌列表中的最后一项时,你可以用这25个令牌帐户重新填充此列表,对。美妙之处在于,如果您有足够数量的账户(代币),每次充值循环,系统会自动等待恢复使用的账户,因此您不必担心速率限制,您的应用程序永不停止因为速率限制问题。我已经在我的项目中实现了这个机制,而且效果很好。