python - 使用 pyGithub 在经过身份验证的用户上创建 repo 会引发异常
问题描述
一切都很顺利,直到我从 get_user()调用AuthenticatedUser上的 create_repo。无论我尝试什么,它总是会引发异常:
[ERROR tornado.application web:1599] Exception in exception handler Traceback (most recent call last): File "/Users/marcocano/Desktop/tsri/discovery-app/discovery/web/api/github.py", line 43, in post
repo = auth_user.create_repo(repo_name) File "/Users/marcocano/Desktop/tsri/discovery-app/env/lib/python3.6/site-packages/github/AuthenticatedUser.py", line 679, in create_repo
"POST", "/user/repos", input=post_parameters File "/Users/marcocano/Desktop/tsri/discovery-app/env/lib/python3.6/site-packages/github/Requester.py", line 319, in requestJsonAndCheck
verb, url, parameters, headers, input, self.__customConnection(url) File "/Users/marcocano/Desktop/tsri/discovery-app/env/lib/python3.6/site-packages/github/Requester.py", line 342, in __check
raise self.__createException(status, responseHeaders, output) github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"}
代码:
if self.request.headers['Content-Type'] == 'application/json':
self.args = json.loads(self.request.body)
repo_name = self.args.get('name', None)
if not repo_name:
raise HTTPError(400, reason=f"Required parameter 'name' not provided")
user = json.loads(self.get_secure_cookie("user"))
# signin with token
g = Github(user['access_token'])
# authenticated user
auth_user = g.get_user()
if auth_user.login:
try:
print('auth_user',auth_user)
repo = auth_user.create_repo(repo_name)
print(repo)
except GithubException as e:
raise HTTPError(400, reason=e)
except Exception as exc: # unexpected
raise HTTPError(500, reason=str(exc))
else:
raise HTTPError(400, reason=f"Unable to authenticate user")
return repo
解决方案
问题是我得到的令牌缺少适当的范围,这里是请求正确范围的文档:https ://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/
推荐阅读
- javascript - 根据另一个数组数组过滤对象数组
- html - 如何在导航栏中的链接之间包含短垂直线?
- angular - 上传和验证 - Typescript 和 Angular
- laravel - 使用谷歌邮件在 Laravel 中发送电子邮件时出错
- unity3d - 如何让 Unity 检查器将文件扩展名视为不同的文件扩展名?
- java - 以两次更快的速度连接到数据库还是连接一次并将其存储在本地?
- mysql - 从 reddit json 中提取信息并使用 node.js 将其放入 mysql 数据库中
- php - 在子文件夹中导航主页和页面的最佳做法是什么?
- javascript - 编辑 Google 表格 HTML 嵌入
- matplotlib - plt.subplots() 使用 for 循环返回的 Axes 对象