Search code examples
rubyyoutube-apigoogle-api-ruby-client

Resumable YouTube Data API v3 uploads using Ruby


I am currently using the google-api-ruby-client to upload videos to Youtube API V3, but I can't find a way of getting the Youtube ID that is created by a resumable upload. The code I am trying to use is along the lines of:

media = Google::APIClient::UploadIO.new(file_path, 'application/octet-stream')
yt_response = @client.execute!({
  :api_method => @youtube.videos.insert,
  :parameters => {
    :part => 'snippet,status',
    'uploadType' => 'resumable'
  },
  :body_object => file_details,
  :media => media
})
return JSON.parse(yt_response.response.body)

But unfortunately for resumable uploads, yt_response.response.body is blank. If I change the 'uploadType' to 'multipart' then body is a JSON blob that contains the Youtube ID. The response for a resumable upload though is only the resumable session URI for the upload with an empty body. How do I go from that URI into the Youtube ID I just created?


Solution

  • Synthesizing the info from How to engage a Resumable upload to Google Drive using google-api-ruby client? and the existing multipart upload sample leads to

    videos_insert_response = client.execute!(
      :api_method => youtube.videos.insert,
      :body_object => body,
      :media => Google::APIClient::UploadIO.new(opts[:file], 'video/*'),
      :parameters => {
        'uploadType' => 'resumable',
        :part => body.keys.join(',')
      }
    )
    videos_insert_response.resumable_upload.send_all(client)
    puts "'#{videos_insert_response.data.snippet.title}' (video id: #{videos_insert_response.data.id}) was successfully uploaded."
    

    That worked for me.