Skip to content

Github enterprise -- app.get_access_token fails on Requester.py __makeAbsoluteUrl assertions. #2766

@ruudharing

Description

@ruudharing

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:

  1. url and therefore o.hostname do not reflect the desired my_hostname
  2. o.path is correct for the get_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions