【问题标题】:Receiving Github APi 403 error when I have not exceeded my rate limit当我没有超过我的速率限制时收到 Github APi 403 错误
【发布时间】:2016-11-17 14:45:27
【问题描述】:

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

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

注意 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)

【问题讨论】:

    标签: python github github-api rate-limiting pygithub


    【解决方案1】:

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

    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)
    

    希望你会发现它很有用。

    【讨论】:

    • 嘿法尔汉。谢谢你的回复,我很感激。但是,我提供了凭据 - 请查看我的编辑。我认为您的禁止请求是我没有想到的。我唯一关心的是它不应该在消息中指定我正在发出哪种类型的禁止请求?在我的 403 中,它指定了“消息”:“超过 XXXXX 的 API 速率限制。”
    【解决方案2】:

    所以问题不在于我的速率限制,而在于 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 进行卷曲,因此它没有向我传递正确的信息。使用令牌表明我确实用完了所有请求。

    【讨论】:

    • 最后根据您的Update,问题是您没有观察到正确的数据,因为您的GET rate-limit 调用未经过身份验证?所以真正的问题是你真的达到了极限?
    猜你喜欢
    • 2018-09-23
    • 2021-08-20
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多