admin管理员组文章数量:1315776
With perl module WWW::Mechanize, I am trying to login to DeviantArt. I added relevant post request fields and add to $mech all the necessary headers. And on firefox, I have turned off javascript.enabled, to test whether or not I would be able to login to deviantart with just html (I was able to). However the script doesn't seem to be working, and instead it cannot find the form on the password page. (DeviantArt has a username page then a password one, redirecting to /_sisu/do/step2 and /_sisu/do/signin)
use strict;
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
print "Make sure to enter a user/pass\n";
$mech->get('');
print "Loaded \n";
my $username = ''; #of course enter in a valid user/pass
my $password = '';
my $csrf;
my $lu_token; #this is on the request page
my $lu_token2;
#DeviantArt uses one username screen, then one password screen. The redirect sites are (DeArt)/_sisu/do/step2 AND THEN (DeArt)/_sisu/do/signin
#Username screen
#lu_token, csrf, etc.
my $form = $mech->forms(); #there is only one form on this page
$csrf = $form->[0]->param("csrf_token");
$lu_token = $form->[0]->param("lu_token");
print "Before Entering Form 1 URL: " . $mech->base() . "\n"; #debug
$mech->add_header( 'Connection' => "keep-alive" );
$mech->add_header( 'Content-Type' => "application/x-www-form-urlencoded" );
#$form->[0]->find_input('csrf_token')->{readonly} = undef; #Commented but it doesn't seem to matter, html page doesn't say it's read only. Uncommenting line for strict_forms does produce an error though
$mech->submit_form( #copying word for word what the site says
form_number => 1,
fields => {
referer => ";,
referer_type => "",
csrf_token => "$csrf",
challenge => "0",
lu_token => $lu_token,
username => $username,
remember => "on",
},
#strict_forms => 1,
);
print "Submitted first, " . $mech->base() . "\n";
if($mech->uri =~ /_sisu/){
print "Got to username redirect\n";
}
#Password screen
#csrf, etc.
$form = $mech->forms();
$csrf = $form->[0]->param("csrf_token") . "\n"; #print "found csrf $csrf\n";
$lu_token = $form->[0]->param("lu_token"); #print "found lutoken $lu_token\n";
$lu_token2 = $form->[0]->param("lu_token2"); #print "found lutoken2 $lu_token2\n";
print("Before second form: " . $mech->base(), "\n");
$mech->add_header( 'Connection' => "keep-alive" );
$mech->add_header( 'Content-Type' => "application/x-www-form-urlencoded" );
$form->[0]->action(";);
$mech->submit_form(
form_number => 1,
fields => {
referer => ";,
referer_type => "",
csrf_token => $csrf,
challenge => "0",
lu_token => $lu_token,
lu_token2 => $lu_token2,
username => "",
password => $password,
remember => "on",
},
#strict_forms => 1,
);
#should redirect to (DeArt)/_sisu/do/signin
print("After second form: " . $mech->base(), "\n"); #debug
if ($mech->content() =~ /\Q$username\E/) {
print "Login successful!\n\n";
}
I have tried to use the direct $mech->post method instead of $mech->submit_form but it redirects me back to the login page. Besides why the code is wrong, I have two other questions
- If I can login to a website while javascript is turned off (like with DeviantArt), does that mean I can always login to it through WWW::Mechanize, or similar modules?
- If you uncomment the "strict_forms" on lines 40 and 71, many of the field values give an error, for example: "Input 'referer_type' is readonly' in Mechanize.pm. Looking at Mechanize.pm appears to me that the error means the attribute is readonly. However, using inspect element on the deviantart page doesn't show any readonly attributes, so why is this?
I am aware of selenium and deviantart's oauth but would like mostly to fix the script
本文标签: perlMechanize cannot login to nonjavascript formStack Overflow
版权声明:本文标题:perl - Mechanize cannot login to nonjavascript form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741990992a2409102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论