views:

690

answers:

6

I am trying to write a Perl script to connect to me YouTube account but it doesnt seem to work. Basically I just want to connect to my account but apparently it is not working. I don't even have an idea on how I could debug this! Maybe it is something related to https protocol?

Please enlighten me! Thanks in advance.

use HTTP::Request::Common;
use LWP::UserAgent;
use strict;

my $login="test";
my $pass = "test";
my $res = "";
my $ua = "";

# Create user agent, make it look like FireFox and store cookies
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20051213 Firefox/1.0.7");
$ua->cookie_jar ( {} );

# Request login page 
$res = $ua->request(GET "https://www.google.com/accounts/ServiceLogin?service=youtube&hl=en_US&passive=true&ltmpl=sso&uilel=3&continue=http%3A//www.youtube.com/signup%3Fhl%3Den_US%26warned%3D%26nomobiletemp%3D1%26next%3D/index");
die("ERROR1: GET http://www.youtube.com/login\n") unless ($res->is_success);


# Now we login with our user/pass
$res = $ua->request(
        POST "https://www.google.com/accounts/ServiceLoginAuth?service=youtube",
        Referer => "http://www.youtube.com/login",
        Content_Type => "application/x-www-form-urlencoded",
        Content => [
                currentform     => "login",
                next            => "/index",
                username        => $login,
                password        => $pass,
                action_login    => "Log+In"
        ]
        );

# YouTube redirects (302) to a new page when login is success
# and returns OK (200) if the login failed.
#die("ERROR: Login Failed\n") unless ($res->is_redirect());


print $res->content;
+7  A: 

What data are you trying to grab? Why not just using an existing implementation like WebService::YouTube

Some comments on your code: I always avoided the shortcut $ua->request(GET/POST) method since I always ended up needing more flexibility that only the use of HTTP::Request and HTTP::Response allowed. I always felt the code was cleaner that way too.

Why is your code not working? Who knows. Make sure your cookiejar is adding your cookies to the outgoing HTTP::Request. I'd suggest dumping all your headers when you do it in a browser and compare with the headers and data that libwww is sending. There may be some additional fields that they are checking for that vary for every hit. They may be checking for your UserAgent string. If you are just looking to learn libwww I'd suggest using a different site as a target as I'm sure YouTube has all sort of anti-scripting hardening.

Trey
+4  A: 

Are you using YouTube's stable documented API?

Use an HTTP proxy such as WebScarab to watch the data flow.

Trey's suggestion to use somebody else's CPAN package for the mechanics is a good idea too.

Liudvikas Bukys
A: 

what i am doing is learning the web features of perl, so i dont want to use any library except wwwlib or mechanize to get the job done. how can i just connect to my account using a perl script? this is my objective for now hope someone can post a script or correct mine. thank you guys for you help. i am testing Webscarab now..

fenec
Edit Question, instead of posting an "answer".
Brad Gilbert
A: 

Right right by and large, what you want to do is define a cookiejar for most of these websites that have a redirection login. This is what the package has done. Also the package tunes a lot of the lookups and scrapes based on the youtube spec.

Ajax content for example will be rough since its not there when your scraping

You just picked a somewhat rough page to start out with.

Enjoy

A: 

yes you are right i just figured out that youtube has a special api to manipulate its feature for example if you want to connect with a script you should have a developer key !!! well it seems like i will use the api . just if anyone could find out a way to connect correctly to youtube let me know . thank you guys.

fenec
A: 

I'm actually working on this issue myself. Before, I would suggest read over this the API guide from Google as a good starting reference. If I'm reading it correctly, one begins with passing user credentials through a REST interface to get a Authentication Token. To handle that, I'm using the following:

sub getToken {
  my %parms = @_;
  my $response    = LWP::UserAgent->new->post(
                   'https://www.google.com/youtube/accounts/ClientLogin',
                   [
                         Email   => $parms{'username'},
                         Passwd  => $parms{'password'},
                         service => "youtube",
                         source  => "<<Your Value Here>>",                            
                   ]
    );


    my $content = $response->content;


    my ($auth) = $content =~ /^Auth=(.*)YouTubeUser(.*)$/msg
           or die "Unable to authenticate?\n";
    my ($user) = $content =~ /YouTubeUser=(.*)$/msg
            or die "Could not extract user name from response string. ";

    return ($auth, $user);
}

And I call that from the main part of my program as such:

## Get $AuthToken
my ($AuthToken, $GoogleUserName) = getToken((
                          username => $email, password => $password
                          ));

Once I have these two things -- $AuthToken and $GoogleUserName, I'm still testing the LWP Post. I'm still writing this unit:

sub test {

my %parms = @_;

## Copy file contents. Use, foy's three param open method. 
my $fileSize = -s $parms{'File'};
open(VideoFile, '<', "$parms{'File'}") or die "Can't open $parms{'File'}.";
binmode VideoFile;
read(VideoFile, my $fileContents, $fileSize) or die "Can't read $parms{'File'}";
close VideoFile;




my $r = LWP::UserAgent->new->post(
    "http://uploads.gdata.youtube.com/feeds/api/users/$parms{'user'}/uploads",
    [
        Host              => "uploads.gdata.youtube.com",
        'Authorization'     => "AuthSub token=\"$parms{'auth'}\"",
        'GData-Version'     => "2",
        'X-GData-Key'       => "key=$YouTubeDeveloperKey",
        'Slug'              => "$parms{'File'}",
        'Content-Type'      => "multipart/related; boundary=\"<boundary_string>\"",
        'Content-Length'    => "<content_length>",
        'video_content_type'=> "video/wmv",
        'Connection'        => "close",
        'Content'           => $fileContents
    ]

);


print Dumper(\$r->content)

}

And that is called as

&test((auth=>$Auth, user=>$user, File=>'test.wmv'));
Richard