我正在尝试编写一个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 ..

有帮助吗?

解决方案

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

对您的代码的一些评论:我总是避免使用快捷方式 $ ua-> request(GET / POST)方法,因为我总是需要更多的灵活性,只使用 HTTP :: Request HTTP :: Response 。我总觉得代码也很干净。

为什么你的代码不起作用?谁知道。 确保您的cookiejar将Cookie添加到传出的 HTTP :: Request 。我建议你在浏览器中删除所有标题,并与标题和数据进行比较 libwww 正在发送。他们正在检查的其他字段可能会因每次点击而异。他们可能正在检查您的 UserAgent 字符串。如果您只是想学习 libwww 我d建议使用不同的网站作为目标,因为我确信YouTube具有各种反脚本强化功能。

其他提示

您使用的是 YouTube稳定记录的API 吗?

使用HTTP代理,例如 WebScarab 来观看数据流。

Trey建议使用别人的CPAN软件包作为机制也是个好主意。

从右到右,您要做的是为大多数具有重定向登录的网站定义一个cookiejar。这就是包的功能。此外,该软件包还会根据youtube规范调整大量查找和搜索。

例如,Ajax内容将是粗略的,因为当你的抓取时它不存在

你刚开始选择了一个粗略的页面。

享受

我自己正在研究这个问题。之前,我建议您阅读 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'));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top