为什么我得到了异常" 403存储库访问被阻止''使用pygithub?

时间:2014-08-20 06:48:18

标签: python python-3.x github github-api

我正在尝试通过pygithub来搜索github用户最喜欢的编程语言,但是很奇怪,每次我想要scrapy用户 - ELLIOTTCABLE'reposity我得到了以下例外:

Traceback (most recent call last):
File "/home/gf/KuaiPan/code/Python/Data Mining/test.py", line 14, in <module>
repo = user.get_repo(j)
File "/usr/lib/python3.4/site-packages/github/NamedUser.py", line 449, in get_repo
"/repos/" + self.login + "/" + name
File "/usr/lib/python3.4/site-packages/github/Requester.py", line 169, in requestJsonAndCheck
return self.__check(*self.requestJson(verb, url, parameters, headers, input, cnx))
File "/usr/lib/python3.4/site-packages/github/Requester.py", line 177, in __check
raise self.__createException(status, responseHeaders, output)
github.GithubException.GithubException: 403 {'message': 'Repository access blocked', 'block': {'reason': 'unavailable', 'created_at': '2014-01-31T14:32:14-08:00'}}

我的python代码如下:

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


ACCESS_TOKEN = 'my credential'
client = Github(ACCESS_TOKEN, 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)

1 个答案:

答案 0 :(得分: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)

这应该可以正常工作。