4

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?

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
SimpleTouch
  • 158
  • 5

2 Answers2

1

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.

Community
  • 1
  • 1
Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • Even when I change it to explicitly do `videos_insert_response.resumable_upload.send_all(client)` the response body is coming back as blank and the upload is completing successfully, just like before without the `send_all`. Trying to access `videos_insert_response.data` causes a `ArgumentError:Content-Type not supported for parsing: text/html` exception. What is the `body` that you were testing with? – SimpleTouch May 22 '13 at 20:54
  • The rest of the code is identical to https://developers.google.com/youtube/v3/code_samples/ruby#upload_video Try a `sudo gem update google-api-client`? I'm testing with v0.6.3. – Jeff Posnick May 23 '13 at 14:56
  • Looks like that was the issue, we were on v0.4.4. – SimpleTouch May 24 '13 at 23:28
0

I am doing resumable uploads in chunks using 0.7.1 version of the API and I had to to this to get the ID...

result = videos_insert_response.resumable_upload.send_all(client)
video = JSON.parse(result.response.body)

puts "Video id '#{video['id']}' was successfully uploaded."
janechii
  • 1,055
  • 1
  • 11
  • 26