【问题标题】:How to upload video on YouTube with Ruby如何使用 Ruby 在 YouTube 上上传视频
【发布时间】:2009-11-07 10:22:15
【问题描述】:

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

'MyProject::Google::YouTube 中的 GData::Client::BadRequestError 应将实际视频上传到 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 的推荐/标准方式是什么,你的方法是什么?

更新

将更改应用到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

...检查请求和响应如下所示:

请求:


&lt;GData::HTTP::Request:0x1b8bb44 @method=:post
@url="http://uploads.gdata.youtube.com/feeds/api/users/lancejpollard/uploads"
@body=#&lt;GData::HTTP::MimeBody:0x1b8c738 @parts=[#&lt;GData::HTTP::MimeBodyString:0x1b8c058 @bytes_read=0
@string="--END_OF_PART_30909\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n
<?xml version=\"1.0\"?>\n<entry xmlns=\"http://www.w3.org/2005/Atom\"\n  xmlns:media=\"http://search.yahoo.com/mrss/\"\n  xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">\n  <media:group>\n    <media:title type=\"plain\">Bad Wedding Toast</media:title>\n    <media:description type=\"plain\">\n      I gave a bad toast at my friend's wedding.\n    </media:description>\n    <media:category scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">People</media:category>\n    <media:keywords>toast
wedding</media:keywords>\n  </media:group>\n</entry>
\n\r\n--END_OF_PART_30909\r\nContent-Type: multipart/related\r\nContent-Transfer-Encoding: binary\r\n\r\n"&gt;
#&lt;File:/Users/Lance/Documents/Development/git/thing/spec/fixtures/sample_upload.mp4&gt;
#&lt;GData::HTTP::MimeBodyString:0x1b8c044 @bytes_read=0
@string="\r\n--END_OF_PART_30909--">]
@current_part=0
@boundary="END_OF_PART_30909">
@headers={"Slug"=>"sample_upload.mp4"
"User-Agent"=>"GoogleDataRubyUtil-AnonymousApp"
"GData-Version"=>"2"
"X-GData-Key"=>"key=AI39si7jkhs_ECjF4unOQz8gpWGSKXgq0KJpm8wywkvBSw4s8oJd5p5vkpvURHBNh-hiYJtoKwQqSfot7KoCkeCE32rNcZqMxA"
"Content-Type"=>"multipart/related; boundary=\"END_OF_PART_30909\""
"MIME-Version"=>"1.0"}>

回复:


#<GData::HTTP::Response:0x1b897e0 @body="No file found in upload request."
@headers={"cache-control"=>"no-cache
no-store
must-revalidate"
"connection"=>"close"
"expires"=>"Fri
01 Jan 1990 00:00:00 GMT"
"content-type"=>"text/plain; charset=utf-8"
"date"=>"Fri
11 Dec 2009 02:10:25 GMT"
"server"=>"Upload Server Built on Nov 30 2009 13:21:18 (1259616078)"
"x-xss-protection"=>"0"
"content-length"=>"32"
"pragma"=>"no-cache"}
@status_code=400>

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

【问题讨论】:

    标签: ruby upload youtube gdata


    【解决方案1】:

    不确定你是否解决了这个问题,但我相信 post_file 中的 mime_type 参数应该设置为 video 的 mime_type - 你已经将它设置为“multipart/相关”在您的示例代码中。

    【讨论】:

    • 还没来得及让它工作,我希望这能解决它!谢谢!
    【解决方案2】:

    我很抱歉只是链接,但我认为这可能会有所帮助linky

    【讨论】:

    • 我看到了,谢谢。我觉得 ruby​​ gdata 库已经以某种方式处理了这个......仍然没有弄清楚。
    【解决方案3】:

    我在使用 gdata gem 时遇到了同样的问题。

    这为我解决了问题。

    C:\Ruby\lib\ruby\gems\1.8\gems\gdata-1.1.1\lib\gdata\http

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

    "def wrap_entry(entry, file_mime_type) Wrapped_entry = "--#{@boundary}\r\n" Wrapped_entry += "内容类型:应用程序/原子+xml; 字符集=UTF-8\r\n\r\n" Wrapped_entry += 条目 Wrapped_entry += "\r\n--#{@boundary}\r\n" Wrapped_entry += "内容类型:#{file_mime_type}\r\n" Wrapped_entry += "内容传输编码:二进制\r\n\r\n"
    返回 MimeBodyString.new(wrapped_entry) 结束”

    【讨论】:

    • 如果有帮助,我的代码如下所示 @yt = GData::Client::YouTube.new @yt.clientlogin("name", "pass") @yt.developer_key = "myKey" url = 'uploads.gdata.youtube.com/feeds/api/users/default/uploads' mime_type = "video/mp4" file_path = "test.mp4" response = @yt.post_file(url, file_path, mime_type, metadata).to_xml
    猜你喜欢
    • 2019-10-19
    • 2017-09-11
    • 2018-10-19
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2011-06-22
    相关资源
    最近更新 更多