Resolved

YouTube API OAuth token expiry causes video archiving instead of retry

Python
OAuth
YouTube API
Error Handling
Backend
Retry Logic
Authentication
DevOps

Google OAuth `invalid_grant` errors classified as generic server errors caused videos to archive after 3 retries | Fixed by adding AUTH_ERROR type with indefinite retries and user re-auth instructions

Issue

Videos were incorrectly archived when YouTube OAuth tokens expired or were revoked, despite these being recoverable authentication issues. The system defaulted to treating such errors as generic server errors, which followed a strict 3-retry policy (30 seconds, 2 minutes, and 5 minutes) before permanently archiving the videos. This required manual intervention to recover videos from archive folders and lost upload progress.

The key error pattern was an OAuth authentication failure:

invalid_grant: Token has been expired or revoked

Culprit

The root cause was a lack of specific handling for authentication failures in the error classification logic. All exceptions, including OAuth-specific errors like token expiration, were defaulted to a generic SERVER_ERROR type. This led to:

  1. The system calling credentials.refresh() to attempt token recovery.
  2. The OAuth refresh failing with an invalid_grant exception.
  3. A generic exception handler classifying the error as SERVER_ERROR.
  4. The retry logic for server errors allowing only 3 attempts before triggering archival.
  5. No special handling for authentication error strings such as invalid_grant or token has been expired.

Solution

1. Added AUTH_ERROR Classification

An additional error type, AUTH_ERROR, was introduced to explicitly handle OAuth token expiration or revocation cases that require manual re-authentication.

2. Enhanced Error Detection

The system now checks for OAuth-specific error strings, such as:

  • invalid_grant
  • token has been expired
  • token has been revoked
  • invalid_client
  • unauthorized
  • access_denied
  • invalid_token
  • expired_token

If any of these indicators are found, the error is classified as AUTH_ERROR.

3. Infinite Retry Configuration

For authentication errors:

  • Retry delays were extended to 5 minutes and then 30 minutes, repeating indefinitely.
  • The maximum retry count was set to a practical infinity (999,999 attempts).
  • Clear logging instructions were added to guide users on re-authentication.

4. User Guidance

Users are now provided with actionable steps in the logs, such as:

🔐 Horizontal upload failed (auth_error) - retrying in 300 seconds (attempt 1 of ∞). To resolve: Delete stored credentials and restart to re-authenticate.

Result: Videos remain in the upload queue indefinitely with extended retry delays until manual re-authentication occurs, preserving upload progress and eliminating the need for manual recovery.