9

We are trying to log into our GitHub server using pygithub as follows below. Note that 'token_bla_bla' is the token that we have generated in GitHub for that user:

g = Github(login_or_token='token_bla_bla', base_url='https://github.company.com')

We then attempt to get the user object and print that by doing:

u = g.get_user()

print u

but we get AuthenticatedUser(login=None) printed.

Does anyone know how to resolve this so we can log in, in order to create and work with repos in our company server?

Thanks in advance :)

2
  • It means you didn't login... Commented Oct 2, 2017 at 11:40
  • did you fill the right parameters, like password? Commented Oct 2, 2017 at 11:43

5 Answers 5

7

Encountered the same issue, and after doing some research in the source code I found out that the correct base_url should have the API v3 endpoint appended to it, and not just the URL of the instance as in Github.com.

I.e. the correct way to instantiate the client will be:

g = Github(login_or_token='token_bla_bla', base_url='https://github.company.com/api/v3')

after that, you will be able to authenticate the user, but note that the authentication is lazy, meaning that it happens only when you request an attribute of the user. Therefore, if you'll print the user right after the client.get_user() method, you will still get an AuthenticatedUser instance with login=None. But, if you'll access the user.login attribute directly, the /user API request will be invoked and the parameter will be lazy-loaded.

To summarize:

# create the GH client correctly
g = Github(login_or_token='token_bla_bla', base_url='https://github.company.com/api/v3')
# create an instance of an AuthenticatedUser, still without actually logging in
user = g.get_user()
print(user) # will print 'AuthenticatedUser(login=None)'
# now, invoke the lazy-loading of the user
login = user.login
print(user) # will print 'AuthenticatedUser(login=<username_of_logged_in_user>)'
print(login) # will print <username_of_logged_in_user>
Sign up to request clarification or add additional context in comments.

1 Comment

you Nailed it, nobody has mentioned this, not even the pygithub documentation
1

There are 3 different ways to login to GitHub server

There 3 different ways to login to github an explained below:

using username and password

g = Github("user", "password")

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

and don't forget to import below module from Github import Github

Answering your question: if you are not using GitHub Enterprise You can use the below way to log in

using username and password

g = Github("user", "password")

or using an access token

g = Github("access_token")

For Enterprise github:

g = Github(base_url="https://{hostname}/api/v3", login_or_token="access_token")
u = g.get_user()
print(u)

In your case you have mentioned login url as:

g = Github(login_or_token='token_bla_bla', base_url='https://github.company.com')

instead try login using below:

g = Github(base_url="https://github.company.com/api/v3", login_or_token="access_token")

Comments

1

You would need to pass the github username into the get_user() function to get a NamedUser object. Something like this:

$ user = get_user('my-github-username')

Expected Output should be:

$ print(user)
NamedUser(login="my-github-username")

Incase there is any issue with your token, it would raise BadCredentialException on parameterized function call. There won't be any exception raised incase of non-parameterized function call.

Comments

0

Have faced same issue. Solved it by decoded the token. Should use module base64 and simplecrypt.

Comments

-1

There are three different ways to login as explained here:

g = Github("user", "password")
g = Github("access_token")
g = Github(base_url="https://{hostname}/api/v3", login_or_token="access_token")

As far as I can see, you used the last one but flipped the parameters. Try with the base_url first, then the other two methods, if that doesn't work.

1 Comment

python does not care about the order of the arguments when using kwargs like OP did

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.