Search code examples
pythonpygithub

GitHub "Requires Authentication" error when using PyGithub


I was trying to figure out how to use the PyGithub module, but I keep getting the same error:

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

My code is pretty simple, considering I just started out:

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

The error is when it gets to print(user.name).


Solution

  • Looking at their documentation, it doesn't look like you're initializing the Github class correctly. I would read through that to find more about how to properly setup. The error is pretty clear that you don't have your authentication credentials input properly.

    Example from the documentation:

    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")