【问题标题】:Twitter API update limits error 403Twitter API 更新限制错误 403
【发布时间】:2014-05-08 08:35:53
【问题描述】:

我正在尝试使用 twitter4j api 从 twitter api 检索数据。在检索数据一段时间后,我收到以下错误:

Exception in thread "main" 403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following).
message - User has been suspended.
code - 63

我得出的结论是,上述错误导致 twitter api 支持的调用受到限制。如何创建间隔以便等到可以从 twitter 获取数据?

例如我的代码如下:

            Long l = Long.parseLong(list.get(index));
            TwitterResponse response = twitter.getFollowersIDs(l);
            RateLimitStatus status = response.getRateLimitStatus();
            if (status.getRemaining() < 2) {
                try {
                    System.out.println("status.getSecondsUntilReset()");
                    Thread.sleep(status.getSecondsUntilReset());
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
            User user = twitter.showUser((l));
            //statuses = twitter.getUserTimeline(l);
            JSONObject features = new JSONObject();

            features.put("_id", l);
            score = kloutScore(l);

我的新代码包含一个 if 语句,它检查 status.getRemaining() 是否接近于零,然后等待 15 分钟,这实际上是限制持续时间。但是我遇到了 TwitterResponse response = twitter.getFollowerIDs(l); 的问题我收到了消息:

Exception in thread "main" 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

【问题讨论】:

  • 我的回答包括有关如何避免超出 Twitter 施加的速率限制的说明。但是,您使用的帐户已被 Twitter 暂停。您需要使用其他用户(至少在您当前使用的用户的暂停被解除之前)。

标签: java twitter


【解决方案1】:

对于每个回复,您可以致电getRateLimitStatus() 以获取RateLimitStatusRateLimitStats.getRemaining() 将告诉您该系列调用可用的 API 请求的剩余数量,如果达到零,您可以调用 RateLimitStatus.getSecondsUntilReset(),并在进行其他调用之前至少等待那么长时间。

有关 Twitter 速率限制的信息可在此处找到:https://dev.twitter.com/docs/rate-limiting/1.1

这是一个基本的例子:

do {
    TwitterResponse response = twitter.getFollowersIDs(userId, cursor);
    RateLimitStatus status = response.getRateLimitStatus();
    if(status.getRemaining() == 0) {
        try {
            Thread.sleep(status.getSecondsUntilReset() * 1000);
        }
        catch(InterruptedException e) {
            // ...
        }
    }
} while(cursor > 0);

在您现在提供的代码中,您正在对 Twitter 进行 2 次调用,showUsergetUserTimeLine。您需要在这两个调用之后检查速率限制状态(UserResponseList 都扩展 TwitterResponse 并具有速率限制信息)。这些调用属于 2 个不同的资源族(usersstatuses),每个速率限制窗口(15 分钟)允许调用这两种方法 180 次。

【讨论】:

  • 我没有使用光标。我有一个带有 id 的列表,我从每个 id 的 twitter 中获取数据。
  • 我尝试计算 TwitterResponse response = twitter.getFollowersIDs(Long.parseLong(list.get(index))); RateLimitStatus 状态 = response.getRateLimitStatus();但是我得到了同样的错误,基本上它无法计算 TwitterResponse。
  • 您使用的帐户已被暂停(可能是因为超出了速率限制)。在解除暂停之前,您必须尝试使用​​其他帐户。
  • 您的原始问题不包含代码,所以我只是展示了一个我编写的简单示例。我已经编辑了我的回复,以解决您在编辑中提出的其他问题。
  • 如何在不使用光标的情况下使用你的 do-while?
【解决方案2】:

我遇到了同样的错误,升级到最新版本的 twitter4j 3.0.5 解决了我的问题。在这里获取最新消息http://twitter4j.org/en/index.html#download 我应该提到我运行的版本是 twitter4j 3.0.3。因此,通过转到 3.0.5 或 3.0.6 版本解决了我的问题。

【讨论】:

  • 您发布了重复的答案,实际上是不允许的!相反,您应该发布一个答案,并在必要时使用该链接发表评论。
猜你喜欢
  • 2013-04-05
  • 2011-07-04
  • 1970-01-01
  • 2010-11-20
  • 2021-08-20
  • 2017-12-20
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
相关资源
最近更新 更多