From f7af5d954065c43e26b5fbb38f45014a748ee693 Mon Sep 17 00:00:00 2001 From: Erik Cederstrand Date: Fri, 5 Aug 2022 11:31:40 +0200 Subject: [PATCH] fix: Add client_id and client_secret explicitly to session.post() to work around token refresh bug. Fixes #1090 --- exchangelib/util.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/exchangelib/util.py b/exchangelib/util.py index 908dbee1..31df0e35 100644 --- a/exchangelib/util.py +++ b/exchangelib/util.py @@ -22,6 +22,7 @@ from pygments import highlight from pygments.formatters.terminal import TerminalFormatter from pygments.lexers.html import XmlLexer +from requests_oauthlib import OAuth2Session from .errors import ( InvalidTypeError, @@ -839,10 +840,12 @@ def post_ratelimited(protocol, session, url, headers, data, allow_redirects=Fals d_start = time.monotonic() # Always create a dummy response for logging purposes, in case we fail in the following r = DummyResponse(url=url, request_headers=headers) + kwargs = dict(url=url, headers=headers, data=data, allow_redirects=False, timeout=timeout, stream=stream) + if isinstance(session, OAuth2Session): + # Fix token refreshing bug. Reported as https://github.com/requests/requests-oauthlib/issues/498 + kwargs.update(session.auto_refresh_kwargs) try: - r = session.post( - url=url, headers=headers, data=data, allow_redirects=False, timeout=timeout, stream=stream - ) + r = session.post(**kwargs) except TLS_ERRORS as e: # Don't retry on TLS errors. They will most likely be persistent. raise TransportError(str(e))