Twitter使用twitter4j限制价格

时间:2015-03-26 10:27:49

标签: java twitter twitter4j

当使用twitter4j库使用方法Twitter.getFollowersIDs()时,我经常会遇到“超出速率限制”的异常。在调用此方法之前,我会检查我是否已经超出了这样的速率限制:

if(Twitter.getRateLimitStatus().get("/followers/ids").getRemaining() > 0) 

如果我尝试使用方法getRateLimitStatus("/followers/ids"),我会收到错误

{"errors":[{"code":32,"message":"Could not authenticate you."}]}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

根据我的经验,错误是误导性的(可能是因为没有正确抛出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();
    } 

依旧......

相关问题