Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4j - Rate limit exceeded

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.

like image 572
Matt Avatar asked Nov 15 '13 11:11

Matt


1 Answers

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 :

  • REST API Rate Limiting in v1.1 (general topic about rate limiting in the Twitter API)
  • REST API v1.1 Limits per window by resource (rate limits for each endpoint)
  • Error Codes and Responses (which explains to you what is your '88' response code).
like image 175
air-dex Avatar answered Sep 18 '22 18:09

air-dex