Resolved

YouTube OAuth token corruption with pickle and JSON format mismatch

Python
YouTube API
OAuth
Authentication
Debugging
File Handling
JSON
Pickle
Backend

YouTube API token file corruption causing _pickle.UnpicklingError when reading JSON-formatted credentials | Required repeated token regeneration and failed thumbnail uploads due to format incompatibility

Issue

The YouTube API authentication system encountered a critical failure due to mismatched data serialization formats, resulting in corrupted token files and failed thumbnail uploads. Specifically, the thumbnail upload process failed with an error indicating an invalid format, requiring manual intervention to regenerate tokens. This issue caused thumbnails to be processed before associated videos were uploaded, leaving orphaned files and disrupting the automated workflow.

Culprit

The root cause was a format mismatch between components of the upload pipeline. One component stored authentication tokens in JSON format, while another attempted to read them as a serialized binary format, causing parsing errors. Additionally, the upload workflow lacked coordination between video and thumbnail uploads, leading to orphaned thumbnail files.

Solution

The following adjustments resolved the authentication and workflow issues:

  1. Standardized Token Format: Aligned all components to use JSON for token storage and retrieval, eliminating parsing errors.
  2. Unified OAuth Scopes: Consolidated authentication scopes to ensure consistent permissions across all upload operations.
  3. Restructured Workflow: Modified the upload process to ensure videos were uploaded before associated thumbnails, preventing orphaned files. Thumbnail handling was integrated into a central watcher component to maintain consistency.

The revised workflow now handles authentication consistently across all components and ensures coordinated uploads for videos and thumbnails, eliminating corruption and orphaned files.