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

7

我正在通过PyGithub从Github上爬取数据。我的问题是在爬取期间我收到了这个错误:

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

当我curl 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

请注意 Ratelimit 标签:

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)
2个回答

2
我曾经在以前的工作中解决过这个问题... 下面是解决方法:
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)

希望您会发现它有用。


嘿Farhan,感谢您的回复,我很感激。但是,我提供了凭据-请查看我的编辑。我认为您所说的禁止请求是我没有考虑到的。我唯一担心的是,它不应该在消息中指定我正在进行哪种类型的禁止请求吗?在我的403中,它指定了“message”:“超过XXXXX的API速率限制。” - ChillMurray

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)

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

然而,看实际的异常本身,我注意到它是GithubException.GithubException,如果没有触发其他异常,它似乎是一个总异常。

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

不幸的是,这是一个非特定的异常。这暂时回答了我的初始问题。

更新:我也在没有令牌的情况下调用API,所以它没有向我传递正确的信息。使用令牌后,它显示我已经用完了所有的请求。


1
根据您在最后的“更新”,问题是因为您没有观察正确的数据,因为您的“GET rate-limit”调用未经过身份验证?因此,真正的问题确实是您已经达到了限制? - payne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接