# Video uploading in youtube

**URL:** <https://www.ruby-forum.com/t/video-uploading-in-youtube/137466>\
**Category:** Rails\
**Created:** [May 13, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/video-uploading-in-youtube/137466 "2008-05-13T13:29:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumyavenukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@soumyavenukumar](https://www.ruby-forum.com/u/soumyavenukumar)\
**Post date:** [May 13, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/video-uploading-in-youtube/137466/1 "2008-05-13T13:29:30Z")

</div>

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](http://www.google.com)’  
auth\_path = ‘/youtube/accounts/ClientLogin’  
upload\_host = ‘[uploads.gdata.youtube.com](http://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

\<?xml version="1.0"?\>

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/](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

---

<div class="post-metadata">

**Author:** ![soumyavenukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@soumyavenukumar](https://www.ruby-forum.com/u/soumyavenukumar)\
**Post date:** [May 28, 2008, 11:43am UTC](https://www.ruby-forum.com/t/video-uploading-in-youtube/137466/2 "2008-05-28T11:43:13Z")

</div>

Hi,  
I got direct API working with Client Login Authentication in Ruby,  
I am using net/http and net/https . I am including the full program  
code below.

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](http://www.google.com)’  
auth\_path = ‘/youtube/accounts/ClientLogin’  
upload\_host = ‘[uploads.gdata.youtube.com](http://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

\<?xml version="1.0"?\>

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/](http://gdata.youtube.com/schemas/2007/)  
categories.cat”\>People  
\</media:category\>  
media:keywordstesttag1, testtag2\</media:keywords\>  
\</media:group\>  
  
–f93dcbA3  
Content-Type: video/mp4  
Content-Transfer-Encoding: binary

#{video}  
–f93dcbA3–  
DATA  
http = Net::HTTP.new(‘[uploads.gdata.youtube.com](http://uploads.gdata.youtube.com)’)

headers = {  
‘X-GData-Client’ =\>“#{client\_id}”,  
‘X-GData-Key’ =\> “key=#{dev\_id}”,  
‘Slug’ =\> “#{@file}”,  
‘Content-Type’ =\> ‘multipart/related; boundary=“f93dcbA3”’,  
‘Content-Length’ =\> data.length.to\_s,  
‘Connection’ =\> ‘close’,  
‘Authorization’ =\> “GoogleLogin auth=#{token}”  
}

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 )

# \*\*\*

Regards,  
Soumya
