python-o365: Account fails to reconnect using Refresh Token after Access Token expires

Issue: When utilizing the O365 library in Python, I have encountered an issue where the connection is not re-established or refreshed when the access token has expired, even though a valid refresh token is present. If I attempt to use the Account object after an inactivity of more than 60 minutes (post the expiration of the access token), the connection fails. I could not find an inbuilt mechanism within the O365 library to handle this. However, as a workaround, I am able to manually utilize msal to obtain a valid token using auth_app.acquire_token_by_refresh_token(), and then re-establish the connection.

Steps to Reproduce:

  1. Set up a Python application with the O365 library.
  2. Authenticate and establish a connection to O365.
  3. Wait for more than 60 minutes without performing any operations, ensuring the access token expires.
  4. Try making a request using the Account object.
  5. Observe that the connection fails and doesn’t automatically use the refresh token to obtain a new access token.

Expected Behavior: The library should automatically use the available valid refresh token to obtain a new access token and re-establish the connection without manual intervention.

Actual Behavior: The connection fails when the access token expires, even with a valid refresh token available. Manual intervention using msal is required to fix the connection.

Workaround: Using msal directly, I am able to get a valid token with auth_app.acquire_token_by_refresh_token(), allowing me to reconnect successfully.

Environment:

  • O365 library version: 2.0.27
  • MSAL library version: 1.23.0
  • Python version: 3.10
  • Operating System: masOS Ventura 13.4.1

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 2
  • Comments: 15 (10 by maintainers)

Most upvoted comments

I meant to come back here and update that I ended up realizing the issue by directly calling the refresh function in Account.py and facepalming. I retract what I said there and suspect that that was the issue @pungys97 ran into. This can probably be closed @janscas as I can confirm it does work.

Anyways, I think the easiest solution for me is to wrap the whole thing in try/catch and refresh there explicitly.

Sure but O365 should do it automatically.

I’ll investigate further into this.

Thanks

Interesting! Seems like microsoft changed something. I’ll try to fix this