当我未超出速率限制时收到 Github APi 403 错误

Chi*_*ray 7 python github rate-limiting github-api pygithub

我正在通过 PyGithub 从 Github 上抓取数据。我的问题是我在抓取过程中收到此错误:

github.GithubException.GithubException: 403 {'documentation_url': ' https://developer.github.com/v3/#rate-limiting ', 'message': '超出 XXXXX 的 API 速率限制。'}

卷曲 api 后我收到:

curl -i https://api.github.com/users/XXXXXX
HTTP/1.1 200 OK
Server: GitHub.com
Date: Thu, 14 Jul 2016 15:03:51 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 1301
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 52
X-RateLimit-Reset: 1468509718
Cache-Control: public, max-age=60, s-maxage=60
Vary: Accept
Last-Modified: Wed, 08 Jun 2016 13:29:08 GMT
Run Code Online (Sandbox Code Playgroud)

请注意速率限制标签:

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 52
X-RateLimit-Reset: 1468509718
Run Code Online (Sandbox Code Playgroud)

如果我再次运行我的 Python 程序,我将收到另一条超出 API 速率限制的消息。我阅读了 github 的 API 文档,据我所知 - 我还剩下 52 个请求。如果我可以提供更多信息以使其更好,请告诉我。谢谢。

编辑:为了澄清我正在使用凭据登录 github。

ORGANIZATION = "ORG"
PERSONAL_ACCESS_TOKEN = "TOKEN"
g = Github(PERSONAL_ACCESS_TOKEN, per_page = 100)
github_organization = g.get_organization(ORGANIZATION)
Run Code Online (Sandbox Code Playgroud)

Chi*_*ray 1

所以问题不在于我的速率限制,而在于 PyGithub 包装器返回的消息。我追溯到我的错误并在源代码中找到了这个类:https://github.com/PyGithub/PyGithub/blob/master/github/Requester.py

在进入 __createException 函数后,我注意到了这一点:

def __createException(self, status, headers, output):
    if status == 401 and output.get("message") == "Bad credentials":
        cls = GithubException.BadCredentialsException
    elif status == 401 and 'x-github-otp' in headers and re.match(r'.*required.*', headers['x-github-otp']):
        cls = GithubException.TwoFactorException  # pragma no cover (Should be covered)
    elif status == 403 and output.get("message").startswith("Missing or invalid User Agent string"):
        cls = GithubException.BadUserAgentException
    elif status == 403 and output.get("message").startswith("API Rate Limit Exceeded"):
        cls = GithubException.RateLimitExceededException
    elif status == 404 and output.get("message") == "Not Found":
        cls = GithubException.UnknownObjectException
    else:
        cls = GithubException.GithubException
    return cls(status, output)
Run Code Online (Sandbox Code Playgroud)

查看收到的异常消息,我认为它是 RateLimitExceededException。

然而,查看实际的异常本身,我注意到它是 GithubException.GithubException,如果没有触发其他异常,它看起来就是一个全面的异常。

这回答了我的问题,因为这不是 API 速率超出问题,因为当我收到此异常时,我仍然有更多请求。

不幸的是,这是一个非特定的例外。这暂时回答了我最初的问题。

更新:我还在没有令牌的情况下卷曲 API,因此它没有向我转发正确的信息。有了令牌,它表明我确实用完了所有请求。