[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Seeking some guidance with net/http

Andrew Cowan

4/4/2008 12:48:00 AM

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', 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...,
'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

2 Answers

Shawn Anderson

4/4/2008 12:53:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

You may want to check out:
http://youtube-g.ruby...

at least see how they manage the authentication aspect.

/Shawn

On Thu, Apr 3, 2008 at 5:48 PM, Andrew Cowan <icculus@gmdstudios.com> wrote:

> 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', 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...,
> '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
>
>

Andrew Cowan

4/4/2008 1:06:00 AM

0

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


Shawn Anderson wrote:
> You may want to check out:
> http://youtube-g.ruby...
>
> at least see how they manage the authentication aspect.
>
> /Shawn
>
> On Thu, Apr 3, 2008 at 5:48 PM, Andrew Cowan <icculus@gmdstudios.com> wrote:
>
>
>> 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', 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...,
>> '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
>>
>>
>>
>
>