How can I login to YouTube using Perl?

I am trying to write a Perl script to connect a YouTube account to me, but it does not seem to work. Basically, I just want to connect to my account, but apparently it does not work. I don't even have an idea on how I could debug this! Maybe this is due to the 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;

what I am doing is learning perl web functions, so I don’t want to use any library other than wwwlib or mechanize to do this job. how can i connect to my account using perl script? this is my goal at the moment hope someone can post a script or fix mine. thanks guys for the help. I am testing Webscarab now.

+3
4

? , WebService::YouTube

: $ua->request(GET/POST), , HTTP::Request HTTP::Response . , .

? . , cookiejar cookie HTTP::Request. , , , libwww. , , . UserAgent. libwww , , YouTube - .

+9

API YouTube?

- HTTP, WebScarab.

Trey - CPAN - .

+5

, , , cookiejar -, . , . scrapes, youtube.

Ajax, , , ,

, .

Enjoy

+1

. Google . , REST, . , :

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);
}

:

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

: $AuthToken $GoogleUserName, LWP. :

sub test {

my %parms = @_;

## Copy file contents. Use, foy 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)
}

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

Source: https://habr.com/ru/post/1707360/


All Articles