# Seeking some guidance with net/http

**URL:** <https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828>\
**Category:** Ruby\
**Created:** [April 4, 2008, 2:48am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828 "2008-04-04T02:48:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 2:48am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/1 "2008-04-04T02:48:59Z")

</div>

Heya, I am attempting to write a script that lets me login to my Youtube  
account to gather frequent data for the videos I have uploaded, I am  
stumbling over the Youtube login procedure which seems to involve both  
cookies and session info. I am able to get this login procedure to work  
with Perl and LWP::UserAgent but so far using net/http and Ruby I am  
failing.

Here is the code I have been playing with which involves first a POST  
form submission to youtube’s /login script, then the code to follow the  
redirection back to the /index page – but looking at the contents  
returned from /index it appears youtube doesn’t recognize me as being  
logged in:

* * *

# Note: I have added a bunch of puts statements to help debug, leaving

them here for this post  
require ‘net/http’

http = Net::HTTP.new( ‘[www.youtube.com](http://www.youtube.com)’, 80 )  
path = ‘/login’  
agent = ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1)  
Gecko/20060111 Firefox/1.5.0.1’

data =  
‘nexturl=/&current\_form=loginForm&action\_login=Log+In&username=MY\_USER&password=MY\_PWD’

headers = {  
‘Referer’ =\> ‘[http://www.youtube.com/](http://www.youtube.com/)’,  
‘Content-Type’ =\> ‘application/x-www-form-urlencoded’,  
‘User-Agent’ =\> agent  
}

resp, data = http.post( path, data, headers )  
cookie = resp.response[‘set-cookie’]

if ( resp.code =~ /^3/ )  
puts "Got Redirect: " + resp[‘location’]  
end

next\_loc = resp[‘location’]

puts ‘Logging In to Set Cookies:’  
puts ‘Code=’ + resp.code  
puts ‘Mesg=’ + resp.message  
puts ‘Cookie=’ + cookie  
puts ‘Next Location=’ + next\_loc  
puts ‘Data=’ + data

headers = {  
‘Cookie’ =\> cookie,  
‘User-Agent’ =\> agent  
}

resp, data = http.get( next\_loc, headers )

puts ‘(should be) Logged In:’  
puts ‘Code=’ + resp.code  
puts ‘Mesg=’ + resp.message  
puts ‘Data=’ + data

* * *

I am unsure what part I am missing which youtube apparently needs to  
complete its login procedure, I would greatly appreciate any helpful  
suggestions!

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 2:54am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/2 "2008-04-04T02:54:16Z")

</div>

You may want to check out:  
[http://youtube-g.rubyforge.org/](http://youtube-g.rubyforge.org/)

at least see how they manage the authentication aspect.

/Shawn

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 2:57am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/3 "2008-04-04T02:57:11Z")

</div>

Try this: [http://rubyforge.org/projects/youtube](http://rubyforge.org/projects/youtube)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 3:06am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/4 "2008-04-04T03:06:34Z")

</div>

Hiya Shawn, I did have a look at youtube-g, have the README file open as  
I type this in fact but I don’t see anything related to authenticating  
an actual Youtube account, the lib is more for accessing the functions  
that Youtube makes available with just guest access as far as I can  
tell… ☹

-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 3:29am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/5 "2008-04-04T03:29:28Z")

</div>

Dan D. wrote:

> Try this: [http://rubyforge.org/projects/youtube](http://rubyforge.org/projects/youtube)

Dan, like the youtube-g lib this one also provides no means for  
authenticating a Youtube account. Though this does provide some  
interesting options that can help me in some respects, it doesn’t  
provide me with the same functionality that I can have as if I were  
actually logged into the website, which I am trying to emulate.

Thanks  
-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 3:54am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/6 "2008-04-04T03:54:08Z")

</div>

This worked for me:

require ‘rubygems’  
require ‘mechanize’

user = “user\_here”  
pw = “password\_here”  
agent = WWW::Mechanize.new  
page = agent.post ‘[YouTube](http://youtube.com/signup)’,  
{“username”=\>user,“password”=\>pw,“current\_form”=\>“loginForm”,“action\_login”=\>“Log+In”}  
page = agent.get ‘[http://youtube.com/](http://youtube.com/)’

# this returns true, showing you are logged in

p page.body.to\_s.include?(“/user/#{user}”)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 4:21am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/7 "2008-04-04T04:21:40Z")

</div>

Shawn, I had not heard of the mechanize lib, but sure enough it works  
great. Thank you for posting that, much appreciated!

Thanks  
-Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 4, 2008, 5:59am UTC](https://www.ruby-forum.com/t/seeking-some-guidance-with-net-http/133828/8 "2008-04-04T05:59:20Z")

</div>

Andrew C. wrote:

> Shawn, I had not heard of the mechanize lib, but sure enough it works  
> great. Thank you for posting that, much appreciated!
> 
> Thanks  
> -Andy

perl has mechanize too.
