Hi,
I am having troubles with direct API working, I
am using net/http and net/https and am getting a Connection reset by
peer error. I am including the full program code below and I hope
somebody can spot
what I am doing wrong.
ruby youtube_upload.rb DevID User Pass Video_Filename
Here is the program code:
require ‘net/http’
require ‘net/https’
if ( ARGV.size < 4 )
print “\nUsage: ruby youtube_upload.rb
\n\n”
exit
end
dev_id = ARGV[0]
user = ARGV[1]
pass = ARGV[2]
@file = ARGV[3]
auth_host = ‘www.google.com’
auth_path = ‘/youtube/accounts/ClientLogin’
upload_host = ‘uploads.gdata.youtube.com’
upload_path = “/feeds/api/users/#{user}/uploads”
video_file = “/home/gmd/vtrack/#{@file}”
if ( !File.exists? video_file )
print “\nError: Invalid video filename specified\n\n”
exit
end
Engage Youtube clientLogin authentication
def init_youtube_auth( host, path, user, pwd, did )
http = Net::HTTP.new( host, 443 )
http.use_ssl = true
POST request → logging in
data = “Email=#{user}&Passwd=#{pwd}&service=youtube&source=VTrack”
headers = {
‘Content-Type’ => ‘application/x-www-form-urlencoded’
}
resp, data = http.post( path, data, headers )
if ( resp.code == ‘200’ )
token = data.scan( /Auth=(.)/ )
user_verify = data.scan( /YouTubeUser=(.)/ )
return token
end
return nil
end
Read a binary file, return contents as string
def read_file( fname )
file_cont = File.open( fname, “r” ) {|io| io.read}
return file_cont
end
Enage Youtube direct upload API
def youtube_direct_upload( host, path, dev_id, token, video )
data = <<“DATA”
–f93dcbA3
Content-Type: application/atom+xml; charset=UTF-8
media:group
<media:title type=“plain”>Video Upload Test</media:title>
<media:description type=“plain”>
Video Upload Test Description
</media:description>
<media:category
scheme=“http://gdata.youtube.com/schemas/2007/
categories.cat”>People
</media:category>
media:keywordstesttag1, testtag2</media:keywords>
</media:group>
–f93dcbA3
Content-Type: video/mpg
Content-Transfer-Encoding: binary
#{video}
–f93dcbA3–
DATA
http = Net::HTTP.new( host, 443 )
http.use_ssl = false
headers = {
‘Host’ => “#{host}”,
‘Authorization’ => “GoogleLogin auth=#{token}”,
‘X-GData-Key’ => “key=#{dev_id}”,
‘Slug’ => “#{@file}”,
‘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
token = init_youtube_auth( auth_host, auth_path, user, pass, dev_id )
if ( token.nil? )
print “\nError: obtaining Authentication Token!\n\n”;
exit(0)
end
print “Auth Token:\n#{token}\n”;
video = read_file( video_file )
resp = youtube_direct_upload( upload_host, upload_path, dev_id, token,
video )
***Error Message
Connection reset by peer
RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace
/usr/local/lib/ruby/1.8/net/protocol.rb:175:in write' /usr/local/lib/ruby/1.8/net/protocol.rb:175:in
write0’
/usr/local/lib/ruby/1.8/net/protocol.rb:151:in write' /usr/local/lib/ruby/1.8/net/protocol.rb:166:in
writing’
/usr/local/lib/ruby/1.8/net/protocol.rb:150:in write' /usr/local/lib/ruby/1.8/net/http.rb:1537:in
send_request_with_body’
/usr/local/lib/ruby/1.8/net/http.rb:1522:in exec' /usr/local/lib/ruby/1.8/net/http.rb:1045:in
request’
/usr/local/lib/ruby/1.8/net/http.rb:1034:in request' /usr/local/lib/ruby/1.8/net/http.rb:543:in
start’
/usr/local/lib/ruby/1.8/net/http.rb:1032:in request' /usr/local/lib/ruby/1.8/net/http.rb:842:in
post’
/home/soumya1/cihighsports2/app/controllers/videos_controller.rb:162:in
youtube_direct_upload' -e:4:in
load’
-e:4
Any help would be appreciated!
Thanks,
Soumya