-
-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expired OAuth token refreshing endless loop #1090
Comments
I solved it with a monkey patch like so: import functools
from exchangelib.services import common as exchangelib_common
from exchangelib.util import post_ratelimited
def extended_post_ratelimited(protocol, session, *args, **kwargs):
session.post = functools.partial(
session.post,
client_id=protocol.credentials.client_id,
client_secret=protocol.credentials.client_secret,
)
return post_ratelimited(protocol, session, *args, **kwargs)
# Monkey-patching infinite loop fix on token refresh, due to OAuth2 library bug when
# passing the app credentials to the "/token" endpoint.
exchangelib_common.post_ratelimited = extended_post_ratelimited While the client credentials are sent correctly as observed in the library's procotol.py: session_params.update(
{
"auto_refresh_kwargs": {
"client_id": self.credentials.client_id,
"client_secret": self.credentials.client_secret,
},
"auto_refresh_url": token_url,
"token_updater": self.credentials.on_token_auto_refreshed,
}
) during refresh they are encoded in the body sent in the It might be a problem outside of this library, actually inside requests-oauthlib under kwargs.update(self.auto_refresh_kwargs)
body = self._client.prepare_refresh_body(
body=body, refresh_token=refresh_token, scope=self.scope, **kwargs
) place where the Reported it here as well, but this might go further into |
Thanks for the detailed write-up! Let's wait to see if the requests-oauthlib issue gets any responses. Otherwise, we can have a look at patches to exchangelib. |
…work around token refresh bug. Fixes #1090
Sure, I'll test it with that patched util.py and report back how it goes. |
Seems to end up on looping with that patch as well. Exact behavior.
|
Does the same happen with the monkey patch from @cmin764 ? In addition to this issue, you may be hitting a possible memory leak. You can try my suggestion in #1100 (comment) which might be a workaround. |
I will test that memory leak workaround and if it's not working then the monkey patch. Memory leak workaround didn't help:
|
I wasn't able to get monkey patch working. Same issue, in endless loop. |
@ecederstrand Btw I am using BackendApplicationClient, not WebApplicationClient on these tests, which might handle token refresh differently (not using refreshtoken, instead requests completely new one with the new session).
I managed to pull full mailbox including 24614 items in several gigabytes. PS. Issue closed by misclick |
Thanks for the find and for testing! I believe #1105 would be a more correct fix. Can you give it a spin before I merge? |
@ecederstrand 🎉 I just gave it a try and it works now flawlessly, thank you! Here's how I tested:
So that patch works, maybe @Tepe has a different use-case where the other change (with session cleanup) is required too. Please let me know about the library version as soon as you release so we can refactor our bot. Thx! |
Thanks for the feedback! Version 4.7.5 is out now. |
@ecederstrand Thank you! Tested with it and it doesn't work since I made a terrible confusion between the PR comments, exactly the other one was the one fixing the problem: #1104 |
Thank you! Would a 4.7.6 be out soon? |
It's out now. |
Thank you @ecederstrand , although I can't see any such release yet:
|
It turns out you need to actually build the new package before uploading to PyPI. Otherwise you're just re-uploading 4.7.5 :-) It's there now. |
🎉 Works flawlessly with 4.7.6. Thank you! |
OAuth token refresh is not working properly. It keeps looping with 100% CPU load and creates million lines of debug output in a short time. It seems to get new session number but access_token itself stays as same and therefore keeps trying to refresh it again and again.
To reproduce you need to have an session which takes longer than OAuth expire time lasts (1 hour, e.g. big mailbox export).
MS documentation of the token refresh:
https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#refresh-the-access-token
DEBUG LOG:
VERSIONS:
Python 3.9.10
exchangelib 4.7.3
The text was updated successfully, but these errors were encountered: