-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Github enterprise -- app.get_access_token fails on Requester.py __makeAbsoluteUrl assertions. #2766
Description
Hi,
Running with PyGithub v1.59.1 = "latest".
Trying to follow the example in https://pygithub.readthedocs.io/en/latest/examples/Authentication.html#app-user-authentication
but for my company's internal GitHub Enterprise installation.
my_base_url="https://" + my_hostname + "/api/v3"
gh = Github( base_url=my_base_url )
app = gh.get_oauth_application( client_info[ 'client_id' ], client_info[ 'client_secret'] )
token = app.get_access_token( code_string )
The last line fails with:
Traceback (most recent call last):
... my code path ...
token = app.get_access_token( code_string )
File "****/.local/lib/python3.8/site-packages/github/ApplicationOAuth.py", line 101, in get_access_token
*self._requester.requestJsonAndCheck(
File "****/.local/lib/python3.8/site-packages/github/Requester.py", line 443, in requestJsonAndCheck
*self.requestJson(
File "****/.local/lib/python3.8/site-packages/github/Requester.py", line 580, in requestJson
return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
File "****/.local/lib/python3.8/site-packages/github/Requester.py", line 676, in __requestEncode
url = self.__makeAbsoluteUrl(url)
File "****/.local/lib/python3.8/site-packages/github/Requester.py", line 784, in __makeAbsoluteUrl
assert o.path.startswith((self.__prefix, "/api/"))
AssertionError
Instrumenting my local copy of Requester.py, function __makeAbsoluteUrl(), with some print statements, just before the fail point, reveals the following:
__makeAbsoluteUrl -- url=https://github.com/login/oauth/access_token
__makeAbsoluteUrl -- o.hostname=github.com
__makeAbsoluteUrl -- o.path=/login/oauth/access_token
This shows two separate problems:
urland thereforeo.hostnamedo not reflect the desiredmy_hostnameo.pathis correct for theget_access_token()operation, but does not start with"/api/"and thus fails the assertion in line 784.
Tracing further back, I see that problem 1 is caused by the hard-coded url in ApplicationOAuth.py line 100 ff,
which does not allow flexibility in hostname:
headers, data = self._checkError(
*self._requester.requestJsonAndCheck(
"POST",
"https://github.com/login/oauth/access_token",
headers={"Accept": "application/json"},
input=post_parameters,
)
)
I notice that the same problems occur with ApplicationOAuth.py function refresh_access_token().
For now, I bypassed these functions. I can get an access+refresh token by using the requests library to post to the correct url,
and then directly building auth = Auth.AppUserAuth( .... ) according to the template in ApplicationOAuth.py line 123 ff.