8

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

gio
  • 81
  • 1
  • 2

5 Answers5

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>
Chaos Monkey
  • 964
  • 1
  • 6
  • 18
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")
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
mohit sehrawat
  • 171
  • 1
  • 12
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.

sxddhxrthx
  • 587
  • 5
  • 13
0

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

qloveshmily
  • 1,017
  • 9
  • 5
-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.

Asclepius
  • 57,944
  • 17
  • 167
  • 143