当我没有超过我的速率限制时收到 Github APi 403 错误

Receiving Github APi 403 error when I have not exceeded my rate limit

我正在通过 PyGithub 从 Github scraping 数据。我的问题是我在 scraping:

期间收到此错误

github.GithubException.GithubException: 403 {'documentation_url': 'https://developer.github.com/v3/#rate-limiting', 'message': 'API rate limit exceeded for XXXXX.'}

卷曲 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

注意速率限制标签:

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 52
X-RateLimit-Reset: 1468509718

如果我再次 运行 我的 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)

我已经用我以前的工作解决了这个问题...在这里..

403 HTTP 状态表示请求被禁止,因此您提供的凭据无法让您访问某些端点。

因此您可能需要在创建 Github 对象时提供有效的凭据(用户名/密码):

#!/usr/bin/env python3
from github import Github

ACCESS_USERNAME = 'username'
ACCESS_PWD = "password"
client = Github(ACCESS_USERNAME, ACCESS_PWD, per_page=100)
user = client.get_user('ELLIOTTCABLE')
repo_list = [repo.name for repo in user.get_repos() if not repo.fork]
print(repo_list)

for j in repo_list:
    repo = user.get_repo(j)
    lang = repo.language
    print(j,':',lang)

希望您会发现它有用。

所以问题不在于我的速率限制,而在于 PyGithub 包装器返回的消息。我追溯了我的错误并在源代码中发现了这个 class :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)

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

但是,查看实际的异常本身,我注意到它是 GithubException.GithubException,如果触发其他异常中的 none,它看起来是一个全面的异常。

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

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

更新:我也在没有令牌的情况下卷曲 API,所以它没有向我传递正确的信息。使用令牌,它表明我确实用完了所有请求。