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

4 python pygithub

我试图弄清楚如何使用 PyGithub 模块,但我不断收到相同的错误:

github.GithubException.GithubException:401 {“message”:“需要身份验证”,“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)
Run Code Online (Sandbox Code Playgroud)

错误发生在到达 时print(user.name)

sed*_*idw 5

查看他们的文档,您似乎没有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")
Run Code Online (Sandbox Code Playgroud)