如何使用 Perl 登录 YouTube?

发布于 2024-07-18 01:15:08 字数 1767 浏览 3 评论 0原文

我正在尝试编写一个 Perl 脚本来连接到我的 YouTube 帐户,但它似乎不起作用。 基本上我只想连接到我的帐户,但显然它不起作用。 我什至不知道如何调试它! 也许这与 https 协议有关?

请赐教! 提前致谢。

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;

我正在做的是学习 Perl 的 Web 功能,所以我不想使用除 wwwlib 或 mechanize 之外的任何库来完成工作。 我怎样才能使用 perl 脚本连接到我的帐户? 这是我现在的目标 希望有人可以发布脚本或纠正我的。 谢谢你们的帮助。 我现在正在测试 Webscarab..

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<mpl=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 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留蓝 2024-07-25 01:15:08

你想抓取什么数据? 为什么不直接使用现有的实现,例如 WebService::YouTube

对您的代码的一些评论:我总是避免使用快捷方式 $ua->request(GET/POST) 方法,因为我总是最终需要比仅使用 HTTP::RequestHTTP::Response 允许。 我总觉得这样代码也更干净。

为什么你的代码不起作用? 谁知道。
确保您的 cookiejar 将您的 cookie 添加到传出 HTTP::Request。 我建议您在浏览器中执行此操作时转储所有标头,并与 libwww 正在发送。 他们可能正在检查一些附加字段,这些字段每次点击都会有所不同。 他们可能正在检查您的 UserAgent 字符串。 如果您只是想学习 libwww 我'我建议使用不同的网站作为目标,因为我确信 YouTube 具有各种反脚本强化功能。

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.

请你别敷衍 2024-07-25 01:15:08

您是否使用 YouTube 的稳定记录 API

使用 HTTP 代理(例如 WebScarab)来监视数据流。

Trey 建议使用其他人的 CPAN 包作为机制也是一个好主意。

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.

勿忘初心 2024-07-25 01:15:08

总的来说,您想要做的就是为大多数具有重定向登录的网站定义一个 cookiejar。 这就是该包所做的。 此外,该软件包还根据 YouTube 规范调整了许多查找和抓取。

例如,Ajax 内容将很粗糙,因为当您刚开始抓取时,它不存在,

您只是选择了一个有点粗糙的页面。

享受

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

眼睛会笑 2024-07-25 01:15:08

我自己实际上也在研究这个问题。 在此之前,我建议您阅读一下来自的 API 指南谷歌是一个很好的起始参考。 如果我没看错的话,首先会通过 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 Post。 我仍在写这个单元:

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

这就是所谓的

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

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'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文