我正在尝试编写一个 Perl 脚本来连接我的 YouTube 帐户,但它似乎不起作用。基本上我只想连接到我的帐户,但显然它不起作用。我什至不知道如何调试它!也许它与https协议(protocol)有关?
请赐教!提前致谢。
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;
我正在做的是学习 perl 的网络功能,所以我不想使用除 wwwlib 或 mechanize 之外的任何库来完成工作。
如何使用 perl 脚本连接到我的帐户?这是我现在的目标
希望有人可以发布脚本或纠正我的。
谢谢你们的帮助。
我现在正在测试 Webscarab..
最佳答案
你想获取什么数据?为什么不直接使用像 WebService::YouTube
这样的现有实现?
对您的代码的一些评论:我总是避免使用快捷方式 $ua->request(GET/POST)
方法,因为我总是需要更多的灵 active ,而不是只使用 HTTP::Request
和 HTTP::Response
允许。我一直觉得这样的代码也更干净。
为什么你的代码不起作用?谁知道。
确保您的 cookiejar 将您的 cookie 添加到传出的 HTTP::Request
.我建议您在浏览器中转储所有 header 并与 libwww
的 header 和数据进行比较正在发送。他们可能会检查一些额外的字段,这些字段因每次点击而异。他们可能正在检查您的 UserAgent
字符串。如果您只是想学习 libwww
我建议使用不同的网站作为目标,因为我确信 YouTube 具有各种反脚本强化功能。
关于perl - 如何使用 Perl 登录 YouTube?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/803832/