问题
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.
回答1:
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).
回答2:
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.
来源:https://stackoverflow.com/questions/19999779/twitter4j-rate-limit-exceeded