Youtube File upload issue

Hello,

I’m triying to upload video to google api via direct upload
(http://code.google.com/intl/fr/apis/youtube/2.0/developers_guide_protocol.html#Direct_uploading).

I’m using an rails active resource class and would lie to handle the
video uploading in a custom method with Nett::HTTP

Here is google requirement :

POST /feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: AuthSub token=“DXAA…sdb8”
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c…a8dc
Slug: video-test.mp4
Content-Type: multipart/related; boundary=“f93dcbA3”
Content-Length: 1941255
Connection: close

–f93dcbA3
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>


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:keywordstoast, wedding</media:keywords>
</media:group>

–f93dcbA3
Content-Type: video/mp4
Content-Transfer-Encoding: binary

--f93dcbA3--

Here is my request object before to be sent :
(irb)> y request

body: |
–RubyMultipartClient901619ZZZZZ
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>


media:group
<media:title type=“plain”>test 7</media:title>
<media:description type=“plain”></media:description>
<media:category
scheme=“http://gdata.youtube.com/schemas/2007/categories.cat”>Film</media:category>
media:keywords</media:keywords>
</media:group>

–RubyMultipartClient901619ZZZZZ
Content-Type: video/mp4
Content-Transfer-Encoding: binary

–RubyMultipartClient901619ZZZZZ–

body_stream:
header:
slug:

  • 01.mp4
    x-gdata-key:

key=AI39si57I1WXXFZSHlDFjrfUgBapS7DoJ0hPppuqgC2FY_vK-nUQ8Iw7lTUJTdqwSCstyIhSWBk-C9JI1DCQolrmVvTcj6xvLg
content-type:

  • multipart/related; boundary=“RubyMultipartClient901619ZZZZZ”
    authorization:
  • AuthSub token=“1/qqpJVDJ4Gziq6pLv8tmMWgIv4zxvwk3sZP9PiVHlCt4”
    gdata-version:
  • “2”
    content-length:
  • “672”
    method: POST
    path: /feeds/api/users/default/uploads
    request_has_body: true
    response_has_body: true

I triyed to read the file and include its content in the body post, also
tryed to include it after encoding in base64
(Base64.encode64(file.read))

The request is build and sent as follow :

uri =
URI.parse(“http://uploads.gdata.youtube.com/feeds/api/users/default/uploads”)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.initialize_http_header(headers)
request.body = post_body
response = http.request(request)

But it keeps responding :

#<Net::HTTPBadRequest 400 Bad Request readbody=true>

I don’t know what’s wrong i’m completely stuck, could you give me a hand
?

I succeeded to make it work, by setting the post body as follow :

data = %{–bbe873dc\r
Content-Type: application/atom+xml; charset=UTF-8

#{xml_entry}\r
–bbe873dc\r
Content-Type: #{meta[:file].content_type}
Content-Transfer-Encoding: binary

#{meta[:file].read}\r
–bbe873dc–\r\n}

I still don’t understand why it wasn’t working properly before … :confused: