rubygoogle-apiyoutube-data-api

YouTube Data API set_thumbnail generates error - failedPrecondition: Precondition check failed


I've been using the YouTube Data API v3/Google APIs Client Library for Ruby for over a year now to upload, update and delete videos automatically. I frequently receive errors from the API when trying to set the thumbnail image for a video right after it has been uploaded (something about a rate limit being exceeded and having to check some quota), but I am always well under the service quotas, there is no mention of the error in the documentation, and resubmitting the thumbnail always goes through so I just resubmit the thumbnail as needed. Most of the time everything works as expected.

Today, however, every time a video has been uploaded and a thumbnail was submitted, I've gotten a new error: failedPrecondition: Precondition check failed

This time, the error happens every time, and resubmitting does not help at all. Nothing has changed recently in my code, and everything else (uploading the video and the description) works as usual, so it does not seem to be a problem with authentication or a bad request. The images are small (less than 100K).

There is no mention of this exception in the Google developer documentation, so there is no way to know what could be wrong.

The code simply does a service.set_thumbnail(vid, upload_source: file), where vid is the YouTube ID of the video and file is the path to the image file. Again, this was working a couple of days ago.

The exception returned is:

failedPrecondition: Precondition check failed.

/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/http_command.rb:239:in `check_status'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/api_command.rb:135:in `check_status'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/http_command.rb:202:in `process_response'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/upload.rb:174:in `process_response'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/upload.rb:259:in `execute_once'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/http_command.rb:126:in `block (2 levels) in do_retry'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/http_command.rb:123:in `block in do_retry'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/http_command.rb:113:in `do_retry'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/http_command.rb:104:in `execute'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-core-0.11.1/lib/google/apis/core/base_service.rb:418:in `execute_or_queue_command'
/home/uploader/local/ruby-3.2.1/lib/ruby/gems/3.2.0/gems/google-apis-youtube_v3-0.6.0/lib/google/apis/youtube_v3/service.rb:3262:in `set_thumbnail'
/home/uploader/rails/app/controllers/concerns/youtube_handler.rb:63:in `block in set_thumbnail'
/home/uploader/rails/app/controllers/concerns/youtube_handler.rb:62:in `open'
/home/uploader/rails/app/controllers/concerns/youtube_handler.rb:62:in `set_thumbnail'

It looks like the library code tries three times to upload the image and gives up.

Has anyone encountered this error?

UPDATE: I did not mention that I actually upload videos with the same Google APIs client code to two different YouTube channels. Posting thumbnails has been working with both channels until now, and I had assumed that thumbnail upload had stopped working on both channels. It turns out, however, that the client code continues to post thumbnails to one channel with no problems. In other words, it stopped working only on one channel. So perhaps the "Precondition check failed" exception has something to do with the status of the channel. Both channels look the same in the YouTube studio web portal, however (no strikes or any other restrictions that I can see).


Solution

  • The fact that one channel continued working while the other stopped made me wonder if it could have something to do with the authorization process. This seemed unlikely, since creating videos and uploading video files continued to work as usual on both channels; only the thumbnail upload was failing on one channel.

    I figured it couldn't hurt to refresh the access token for the problem channel, and voilĂ ! thumbnails started working as soon as I did that. Something must have gotten corrupted somewhere and generating a new token must have cleared it out.

    I'm leaving this up in case it helps someone else.