使用 PyGithub 时 Github“需要身份验证”错误

问题描述

我试图弄清楚如何使用 PyGithub 模块,但我一直收到同样的错误

github.GithubException.GithubException: 401 {"message": "Requires authentication","documentation_url": "https://docs.github.com/rest/reference/users#get-the-authenticated-user"}

考虑到我刚刚开始,我的代码非常简单:

from github import Github
g = Github("Charonum","xxxxxxxx")
user = g.get_user()
print(user.name)
print(user.login)

错误是当它到达 print(user.name) 时。

解决方法

查看他们的 docs,您似乎没有正确初始化 Github 类。我会通读该文件以了解有关如何正确设置的更多信息。错误很明显,您没有正确输入身份验证凭据

文档中的示例

from github import Github

# using an access token
g = Github("access_token")

# Github Enterprise with custom hostname
g = Github(base_url="https://{hostname}/api/v3",login_or_token="access_token")