Home > Net >  Ruby YouTube Data API v3 insert caption always returns error
Ruby YouTube Data API v3 insert caption always returns error

Time:07-30

I am trying to use the Ruby SDK to upload videos to YouTube automatically. Inserting a video, deleting a video, and setting the thumbnail for a video works fine, but for some reason trying to add captions results in an invalid metadata client error regardless of the parameters I use.

I wrote code based on the documentation and code samples in other languages (I can't find any examples of doing this in Ruby with the current gem). I am using the google-apis-youtube_v3 gem, version 0.22.0.

Here is the relevant part of my code (assuming I have uploaded a video with id 'XYZ123'):

require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'google-apis-youtube_v3'

def authorize [... auth code omitted ...] end

def get_service
  service = Google::Apis::YoutubeV3::YouTubeService.new
  service.key = API_KEY
  service.client_options.application_name = APPLICATION_NAME
  service.authorization = authorize
  service
end

body = {
  "snippet": {
    "videoId": 'XYZ123',
    "language": 'en',
    "name": 'English'
  }
}

s = get_service
s.insert_caption('snippet', body, upload_source: '/path/to/my-captions.vtt')

I have tried many different combinations, but the result is always the same:

Google::Apis::ClientError: invalidMetadata: The request contains invalid metadata values, which prevent the track from being created. Confirm that the request specifies valid values for the snippet.language, snippet.name, and snippet.videoId properties. The snippet.isDraft property can also be included, but it is not required. status_code: 400

It seems that there really is not much choice for the language and video ID values, and there is nothing remarkable about naming the captions as "English". I am really at a loss as to what could be wrong with the values I am passing in.

Incidentally, I get exactly the same response even if I just pass in nil as the body.

CodePudding user response:

I looked at the OVERVIEW.md file included with the google-apis-youtube_v3 gem, and it referred to the Google simple REST client Usage Guide, which in turn mentions that most object properties do not use camel case (which is what the underlying JSON representation uses). Instead, in most cases properties must be sent using Ruby's "snake_case" convention.

Thus it turns out that the snippet should specify video_id and not videoId.

That seems to have let the request go through, so this resolves this issue.

The response I'm getting now has a status of "failed" and a failure reason of "processingFailed", but that may be the subject of another question if I can't figure it out.

  • Related