From: Andrew Cowan Date: 2008-04-17T01:34:10+09:00 Subject: Re: net/http - Confirming the right implementation I wanted to post a followup to this, I finally realized that I was trying to use an ssl connection to the uploads. server for the direct upload api when that was only needed for obtaining the ClientLogin auth key. Replacing that with a normal http:// call solved the issue. -Andy Andrew Cowan wrote: > Hey folks, I am trying to use net/http and net/https to implement a > specific POST call for youtube's video upload service (am trying to > use the direct upload API) and am looking to confirm that I am > implementing the net/http library properly to do this. The full POST > call is as follows (this comes from the youtube API): > > POST /feeds/api/users/default/uploads HTTP/1.1 > Host: uploads.gdata.youtube.com > Authorization: ClientLogin auth=my_auth_token_here > X-GData-Key: key=my_dev_id_here > Slug: videofilename.mpg > Content-Type: multipart/related; boundary="f93dcbA3" > Content-Length: 1941255 > Connection: close > > --f93dcbA3 > Content-Type: application/atom+xml; charset=UTF-8 > > > > xmlns="http://www.w3.org/2005/Atom" > xmlns:media="http://search.yahoo.com/mrss/" > xmlns:yt="http://gdata.youtube.com/schemas/2007"> > > > > > type="plain">My Video Title Here > > type="plain"> > My Video Description Here > > > > scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People > > >cat_keyword1, > cat_keyword2 > > > --f93dcbA3 > Content-Type: video/mp4 > Content-Transfer-Encoding: binary > > full_video_file_binary_contents_here > --f93dcbA3-- > > > > > Here is the code I am using to implement this, am hoping to make sure > that I am using net/http correctly to pass all of the data where it > needs to be. > > dev_id = 'my_YT_dev_id_here' > upload_host = 'uploads.gdata.youtube.com' > upload_path = '/feeds/api/users/my_username_here/uploads' > > > youtube_direct_upload( upload_host, upload_path, dev_id, video ) > > > def youtube_direct_upload( host, path, dev_id, token, video ) > data = <<"DATA" > --f93dcbA3 > Content-Type: application/atom+xml; charset=UTF-8 > > > xmlns:media="http://search.yahoo.com/mrss/" > xmlns:yt="http://gdata.youtube.com/schemas/2007"> > > My Video Test > > Video Upload Test > > scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People > > test, video > > > --f93dcbA3 > Content-Type: video/mpg > Content-Transfer-Encoding: binary > > #{video} > --f93dcbA3-- > DATA > > > http = Net::HTTP.new( host, 443 ) > http.use_ssl = true > > headers = { > 'Host' => "#{host}", > 'Authorization' => "GoogleLogin auth=#{token}", > 'X-GData-Key' => "key=#{dev_id}", > 'Slug' => 'videofile.mpg', > 'Content-Type' => 'multipart/related; boundary="f93dcbA3"', > 'Content-Length' => data.length.to_s, > 'Connection' => 'close' > } > > resp, data = http.post( path, data, headers ) > > puts 'Code: ' + resp.code > puts 'Data: ' > puts data > > end > > > > I left out the code which rerieves the auth_token from the youtube > server as I have tested that aspect and it works fine, only having > trouble getting the above to work, when I try it I get this string of > errors: > > /usr/local/lib/ruby/1.8/openssl/buffering.rb:178:in `syswrite': Broken > pipe (Errno::EPIPE) > from /usr/local/lib/ruby/1.8/openssl/buffering.rb:178:in > `do_write' > from /usr/local/lib/ruby/1.8/openssl/buffering.rb:192:in `write' > from /usr/local/lib/ruby/1.8/net/protocol.rb:175:in `write0' > from /usr/local/lib/ruby/1.8/net/protocol.rb:151:in `write' > from /usr/local/lib/ruby/1.8/net/protocol.rb:166:in `writing' > from /usr/local/lib/ruby/1.8/net/protocol.rb:150:in `write' > from /usr/local/lib/ruby/1.8/net/http.rb:1514:in > `send_request_with_body' > from /usr/local/lib/ruby/1.8/net/http.rb:1496:in `exec' > from /usr/local/lib/ruby/1.8/net/http.rb:1044:in `request' > from /usr/local/lib/ruby/1.8/net/http.rb:1033:in `request' > from /usr/local/lib/ruby/1.8/net/http.rb:545:in `start' > from /usr/local/lib/ruby/1.8/net/http.rb:1031:in `request' > from /usr/local/lib/ruby/1.8/net/http.rb:840:in `post' > from youtube_auth.rb:95:in `youtube_direct_upload' > > > I am hoping that I have simply implemented the net/http library > incorrectly, but am not sure... Any help would be greatly appreciated. > > Thanks, > Andy > > >