Twitter4j - 超出速率限制 [英] Twitter4j - Rate limit exceeded

查看:36
本文介绍了Twitter4j - 超出速率限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Twitter4j 中使用 getFollowersIds() 获得关注者,但我得到 <块引用>

连接错误异常...超出速率限制

public static void main(String[] args) {尝试 {推特推特 = TwitterFactory.getSingleton();String[] srch = new String[]{"TechCrunch"};响应列表<用户>用户 = twitter.lookupUsers(srch);对于(用户用户:用户){UserHarvest us = new UserHarvest(6017542);us.getFollowersIds();尝试 {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);}}

错误信息:

线程main"harvest.twitterharvest.ConnectionErrorException 中的异常:无法建立连接在收获.twitterharvest.WrapperTwitter4J.getFollowersIDs(WrapperTwitter4J.java:75)在收获.twitterharvest.UserHarvest.getFollowersIds(UserHarvest.java:106)在收获.twitterharvest.UserHarvest.main(UserHarvest.java:140)原因: 429:在 API v1.1 中返回,当由于应用程序对资源的速率限制已耗尽而无法提供请求时.请参阅 API v1.1 中的速率限制.(https://dev.twitter.com/docs/rate-limiting/1.1)消息 - 超出速率限制代码 - 88相关讨论可以在互联网上找到:http://www.google.co.jp/search?q=92c30ec6 或http://www.google.co.jp/search?q=19e1da11TwitterException{exceptionCode=[92c30ec6-19e1da11], statusCode=429, message=Rate limit exceeded, code=88, retryAfter=-1, rateLimitStatus=RateLimitStatusJSONImpl{remaining=0, limit=15, resetTimeInSeconds=138451240s版本=3.0.4}在 twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:162)在 twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)在 twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:89)在 twitter4j.TwitterImpl.get(TwitterImpl.java:1894)在 twitter4j.TwitterImpl.getFollowersIDs(TwitterImpl.java:409)在收获.twitterharvest.WrapperTwitter4J.getFollowersIDs(WrapperTwitter4J.java:73)... 2 更多

我看到 secondsUntilReset=904.我在 1 小时后运行代码并收到相同的错误消息.我不知道为什么.感谢您的帮助.

解决方案

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

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

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

有关更多信息,请查看 Twitter 开发人员 上的这些资源,尤其是 它的文档 :

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

ConnectionErrorException ... rate limit exceeded

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.

解决方案

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

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

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.

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

这篇关于Twitter4j - 超出速率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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