I've been using the WWW::Mechanize perl module to try to login to a website that requires Steam authentication. So far I've been able to get it to the login page successfully, but once I submit my form it seems to not work at all. When the form is submitted the content of the page doesn't change as if I failed the login and it wants me to try again. I've been searching for a few hours now and I have tried lots of different combinations of setting the form boxes and submitting the text, but nothing is working. For some reason I've found little about checking if Mechanize can see if the login worked or not. Perhaps my search terms aren't good.

Here is the code I've gotten so far. What this should do is go to tf2wh's login form which redirects to the Steam login page, fill in the user/pass, click the button, then go back to tf2wh with the login session saved in the cookies. However, the only part that works is the initial login redirect.

#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;

my $user = "my_username";
my $pass = "my_password";
my $uri = 'http://www.tf2wh.com/?login';
my $cookies = 'cookies.txt';
my $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';

my $mechanize = WWW::Mechanize->new(
    agent => $agent,
    cookie_jar => {},
    autosave => 1,
    ignore_discard => 1);

$mechanize->add_header(
    "Connection" => "keep-alive",
    "Keep-Alive" => "115");

$mechanize->get( $uri );

$mechanize->success or die "Could not fetch login page.\n";

#One of many different forms I've tried
$mechanize->form_name('login');
$mechanize->set_visible($user, $pass);
$mechanize->click();
$mechanize->follow_link();

$mechanize->success or die 'Could not login.';

print "Logged in successfully! Trying to look at TF2WH now.\n";

#Test to see if we get the "You need to login" message
$mechanize->get('http://www.tf2wh.com/item.php?id=6011;6;78e2c5962db56a60f7c143a12875f3b6');

print "Fetched\n";

if($mechanize->text() =~ m{Handy Hint:})
{
    print "Failed to login.\n";
}
else
{
    print "Up and running!";
}
有帮助吗?

解决方案

You might need to propagate some steam headers.

Single sign-on solutiuons are all full of quirks.

You might have luck looking for a developers guide at steam that describes how to use their authn/authz system in "your game."

If you're unlucky, they've bundled it all into binary objects. If you're lucky, they'll tell you exactly what and how to propagate the login info.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top