Good $localtime dear monks
I am using the MediaWiki API to create a page updating bot. I got through some problems getting my headers accepted thanks to help from bliako in the thread Rest::Client Headers not getting through. But now I still can't get wiki to accept the token it gives me. From some googling I understand you need to get a token, then hand it over along with a login request to link that token to a logged in session. This is all I'm trying here, but when I send the token I just got, I get the error 'WrongToken'
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use MIME::Base64;
use JSON;
use REST::Client;
use Getopt::Long;
use HTTP::Cookies;
use URI::Escape;
use Sys::Hostname;
$|++; # Output buffering off
my $wiki = 'https://wikihost';
my $api = '/mediawiki/api.php';
my $JSON = JSON->new->allow_nonref;
# Create rest client and set some options
my $client = REST::Client->new();
$client->setHost($wiki);
my $ua = $client->getUseragent(); # Get the LWP User agent
$ua->ssl_opts(verify_hostname => 0);
my $cookie_jar = HTTP::Cookies->new(
file => "/tmp/lwp_cookies.dat",
autosave => 1,
ignore_discard => 1,
);
$ua->cookie_jar( $cookie_jar ); # Give the chap a cookie
# First get a token. This works
my $request = 'action=query&meta=tokens&type=login&format=json';
my $result = post($request);
print Dumper $result;
my $token = uri_escape( $result->{query}{tokens}{logintoken} );
print "URI Escaped Token: $token\n";
# Now we log in with that token. I've tried both the below requests
$request = 'action=login&lgname=Marvin&lgpassword=ldiode&lgtoken=$toke
+n&lgdomain=wikihost&format=json';
# $request = 'action=login&lgname=Marvin&lgpassword=ldiode&lgtoken=$to
+ken&format=json';
$result = post($request);
print Dumper $result;
sub post {
my $query = shift @_;
$client->POST($api, $query, {'Content-type' => 'application/x-www-
+form-urlencoded'});
if ( $client->responseCode() == 200 ) {
return $JSON->decode( $client->responseContent() );
}
else {
print "Failed with code $client->responseCode()\n";
print Dumper $client->responseContent();
}
}
And this is the output I get
./TestToken.pl
$VAR1 = {
'batchcomplete' => '',
'query' => {
'tokens' => {
'logintoken' => '3de22ae8b84f7190
+35b25e69ae7b33655b06705a+\\'
}
}
};
URI Escaped Token: 3de22ae8b84f719035b25e69ae7b33655b06705a%2B%5C
$VAR1 = {
'login' => {
'result' => 'WrongToken'
}
};
Does anyone see any obvious errors in my code, or know the wiki API well enough to spot my error. The one thing I am not sure about is cookie handling. I have cargo culted that cookie setup from various example found on t'web. Should that cookie setup be sensible, or an I best to clear the cookie file between runs? Or have the cookies nothing to do with what's going on here?
Thanks in Advance,
R.
Pereant, qui ante nos nostra dixerunt!