如何使用Ruby在YouTube上上传视频

时间:2009-11-07 10:22:16

标签: ruby upload youtube gdata

我正在尝试使用GData gem上传YouTube视频(我已经看过youtube_g gem,但如果可能的话,我想让它与纯GData一起工作),但我不断收到此错误:

'MyProject中的GData :: Client :: BadRequestError :: Google :: YouTube应该将实际视频上传到YouTube(一旦它上传,请模拟此测试) 请求错误400:在上传请求中找不到文件。

我正在使用此代码:


def metadata
  data = <<-EOF
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <media:group>
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    </media:description>
    <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>
    <media:keywords>toast, wedding</media:keywords>
  </media:group>
</entry>
EOF
end

@yt = GData::Client::YouTube.new
@yt.clientlogin("name", "pass")
@yt.developer_key = "myKey"
url = "http://uploads.gdata.youtube.com/feeds/api/users/name/uploads"
mime_type = "multipart/related"
file_path = "sample_upload.mp4"
@yt.post_file(url, file_path, mime_type, metadata)

使用ruby将视频上传到youtube的推荐/标准方式是什么?您的方法是什么?

更新

将更改应用于<?xml version="1.0"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">Bad Wedding Toast</media:title> <media:description type="plain"> I gave a bad toast at my friend's wedding. </media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category> <media:keywords>toast, wedding</media:keywords> </media:group> </entry> EOF end后,它生成的字符串如下所示:


@yt = GData::Client::YouTube.new
@yt.clientlogin("name", "pass")
@yt.developer_key = "myKey"
url = "http://uploads.gdata.youtube.com/feeds/api/users/name/uploads"
mime_type = "multipart/related"
file_path = "sample_upload.mp4"
@yt.post_file(url, file_path, mime_type, metadata)

...并检查请求和响应如下:

请求:

wrapped_entry

响应:


--END_OF_PART_59003
Content-Type: application/atom+xml; charset=UTF-8
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <media:group>
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    </media:description>
    <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>
    <media:keywords>toast, wedding</media:keywords>
  </media:group>
</entry>

--END_OF_PART_59003
Content-Type: multipart/related
Content-Transfer-Encoding: binary

仍然无法正常工作,我将不得不通过这些更改进行更多检查。

3 个答案:

答案 0 :(得分:0)

我为仅仅链接而道歉,但我认为这可能有助于linky

答案 1 :(得分:0)

我和gdata gem有同样的问题。

这为我解决了。

C:\红宝石\ lib中\红宝石\宝石\ 1.8 \宝石\ GDATA-1.1.1 \ lib中\ GDATA \ HTTP

在“MimeBody类”中,用此代码替换“wrap_entry”以修复它。

“def wrap_entry(entry,file_mime_type)         wrapped_entry =“ - #{@ boundary} \ r \ n”         wrapped_entry + =“Content-Type:application / atom + xml; 字符集= UTF-8 \ r \ n \ r \ n”个         wrapped_entry + =条目         wrapped_entry + =“\ r \ n - #{@ boundary} \ r \ n”         wrapped_entry + =“Content-Type:#{file_mime_type} \ r \ n”         wrapped_entry + =“Content-Transfer-Encoding:binary \ r \ n \ r \ n”
        返回MimeBodyString.new(wrapped_entry)       端“

答案 2 :(得分:0)

不确定您是否曾修复此问题,但我认为mime_type中的post_file参数应设置为视频的mime_type - 您已将其设置为在示例代码中使用“multipart / related”。

相关问题