2013-05-15 58 views
4

我目前使用google-api-ruby-client將視頻上傳到Youtube API V3,但是我找不到獲取由可恢復上載創建的Youtube ID的方法。我想使用的代碼是沿着線:使用Ruby恢復可恢復的YouTube數據API v3上傳

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) 

但不幸的是斷點續傳,yt_response.response.body是空白。如果我將'uploadType'更改爲'multipart',則body是包含Youtube ID的JSON blob。雖然對可恢復上載的響應只是上傳時可恢復的會話URI,但爲空。我如何從該URI轉到剛創建的Youtube ID?

回答

1

從合成的How to engage a Resumable upload to Google Drive using google-api-ruby client?信息和existing multipart upload sample導致

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." 

爲我工作。

+0

即使當我改變它明確地做'videos_insert_response.resumable_upload .send_all(客戶端)'響應主體回到空白狀態,上傳成功完成,就像以前沒有'send_all'一樣。嘗試訪問'videos_insert_response.data'會導致'ArgumentError:Content-Type不支持解析:text/html'異常。你正在測試的「身體」是什麼? – SimpleTouch

+0

其餘代碼與https://developers.google.com/youtube/v3/code_samples/ruby#upload_video相同嘗試使用'sudo gem update google-api-client'?我正在測試v0.6.3。 –

+0

看起來像是這個問題,我們在v0.4.4上。 – SimpleTouch

0

我在使用0.7.1版本的API塊做斷點續傳,我只好到這來獲取ID ...

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

puts "Video id '#{video['id']}' was successfully uploaded."