Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login problems using pygithub

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

like image 941
gio Avatar asked Nov 19 '22 08:11

gio


1 Answers

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>
like image 90
Chaos Monkey Avatar answered Dec 06 '22 16:12

Chaos Monkey