PyGithub icon indicating copy to clipboard operation
PyGithub copied to clipboard

System and Github server time difference is causing 401

Open AnkeshThakur opened this issue 1 year ago • 0 comments

During execution when PyGithub needs to refresh token it ran into problem. It was running smoothly before but at that moment started giving 401 errors. What appears to be the reason for this is caused by difference between time of local system and github servers. This error came around midnight local time. Generally it is not observed but for long running processes it might be critical.

Stack trace:

File "/usr/local/lib/python3.11/site-packages/github/Requester.py", line 548, in requestJsonAndCheck
    return self.__check(*self.requestJson(verb, url, parameters, headers, input, self.__customConnection(url)))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/Requester.py", line 713, in requestJson
    return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/Requester.py", line 798, in __requestEncode
    requestHeaders["Authorization"] = f"{self.__auth.token_type} {self.__auth.token}"
                                                                  ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/Auth.py", line 328, in token
    self.__installation_authorization = self._get_installation_authorization()
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/Auth.py", line 339, in _get_installation_authorization
    return self.__integration.get_access_token(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/GithubIntegration.py", line 228, in get_access_token
    headers, response = self.__requester.requestJsonAndCheck(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/Requester.py", line 548, in requestJsonAndCheck
    return self.__check(*self.requestJson(verb, url, parameters, headers, input, self.__customConnection(url)))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/github/Requester.py", line 609, in __check
    raise self.createException(status, responseHeaders, data)
github.GithubException.GithubException: 401 {"message": "'Expiration time' claim ('exp') must be a numeric value representing the future time at which the assertion expires", "documentation_url": "https://docs.github.com/rest", "status": "401"}

A relevant issue issue was resolved here - https://github.com/octokit/auth-app.js/pull/164

The approach identifies the time diff b/w servers and adjust exp/iat accordingly.

AnkeshThakur avatar Dec 11 '24 07:12 AnkeshThakur