Fix: Handle expired OpenEdX refresh tokens gracefully - #3834
Open
sentry[bot] wants to merge 1 commit into
Open
Conversation
OpenAPI ChangesShow/hide changesUnexpected changes? Ensure your branch is up-to-date with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses the
HTTPError: 400 Client Error: Bad Request for url: https://courses.learn.mit.edu/oauth2/access_tokenoccurring in theopenedx.tasks.update_edx_user_profileCelery task.Root Cause:
The error occurs when a user's stored OpenEdX OAuth2 refresh token has expired or been invalidated on the OpenEdX side. When
_refresh_edx_api_authattempts to use this token, the OpenEdX server returns a 400 Bad Request, which was previously unhandled, causing the task to crash.Solution:
openedx/api.py, the_refresh_edx_api_authfunction now catchesHTTPErrorwith a 400 status code. If a 400 is received, it indicates an invalid refresh token.refresh_tokenandaccess_tokenare cleared from theOpenEdxApiAuthrecord. The system then attempts a full re-authorization by callingcreate_edx_auth_token, which simulates a user login to obtain a fresh set of tokens.update_edx_user_profileCelery task inopenedx/tasks.pynow includes a try-except block. This ensures that if any part of the profile update or token re-authorization process fails (even the fallback), the task logs the exception and completes gracefully, preventing repeated crashes for the same user.Fixes MITXONLINE-6EG