Java 使用twitter4j的Twitter速率限制
在使用twitter4j库使用方法Twitter.getFollowersIDs时,我经常会遇到超出异常率限制的情况。在调用此方法之前,我会检查是否已超过速率限制,如下所示:Java 使用twitter4j的Twitter速率限制,java,twitter,twitter4j,Java,Twitter,Twitter4j,在使用twitter4j库使用方法Twitter.getFollowersIDs时,我经常会遇到超出异常率限制的情况。在调用此方法之前,我会检查是否已超过速率限制,如下所示: if(Twitter.getRateLimitStatus().get("/followers/ids").getRemaining() > 0) 如果我尝试使用getRateLimitStatus/followers/ids方法,我会得到一个错误 {"errors":[{"code":32,"message":
if(Twitter.getRateLimitStatus().get("/followers/ids").getRemaining() > 0)
如果我尝试使用getRateLimitStatus/followers/ids方法,我会得到一个错误
{"errors":[{"code":32,"message":"Could not authenticate you."}]}
我做错了什么?根据我的经验,这个错误可能是误导性的,因为GetLeving中的早期错误没有正确抛出。 我做这件事的方式是按照
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();
}
等等…您是否设置了身份验证?见:见我的答案: