[英]Twitter rate limit using twitter4j
当使用通过twitter4j库使用的方法Twitter.getFollowersIDs()时,我经常会遇到异常“超出速率限制”。 在调用此方法之前,请检查是否没有超过速率限制,如下所示:
if(Twitter.getRateLimitStatus().get("/followers/ids").getRemaining() > 0)
如果我尝试使用方法getRateLimitStatus("/followers/ids")
,则会收到错误消息
{"errors":[{"code":32,"message":"Could not authenticate you."}]}
我究竟做错了什么?
根据我的经验,该错误具有误导性(可能是因为未正确引发getRemaining()中的先前错误)。 我这样做的方式是
JSONObject jsonObject = new JSONObject(twitter.getRateLimitStatus().get("/followers/ids"));
try {
int remaining = Integer.parseInt(jsonObject.get("remaining").toString());
if (remaining == 0)
{
int secondsUntilReset = Integer.parseInt(jsonObject.get("secondsUntilReset").toString());
System.out.println("Waiting for seconds: ".concat(jsonObject.get("secondsUntilReset").toString()));
Thread.sleep((( secondsUntilReset ) * 1000));
}
} catch (JSONException e) {
e.printStackTrace();
}
等等...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.