##
The 'token' parameter was found in the query string, but must be in the POST body
##
##
The token parameter must be set
##
##
use strict;
use warnings;
use v5.10.0;
use Data::Dumper;
use MIME::Base64;
use JSON;
use REST::Client;
use Getopt::Long;
my $debug = 0;
GetOptions ("debug" => \$debug) or die "Error in command line arguments\n";
my $wiki = 'http://HeartOfGold.improbable.tea';
my $api = '/mediawiki/api.php';
my $request;
my $JSON = JSON->new->allow_nonref;
# Create rest client and set some options
my $client = REST::Client->new();
$client->setHost($wiki);
$client->addHeader('format', 'json');
# First we get a login token
$request = 'action=login&lgname=Marvin&lgpassword=leftdiode';
my $result = post($request);
my $token = $result->{login}{token};
print "Got Token: $token\n";
$client->addHeader('token', $token);
$request = "action=edit&title=LookupAutomationTest&summary=test%20summary&text=article%20content";
$result = post($request);
sub post {
my $query = shift @_;
$query .= '&format=json';
my $req = "$api?$query";
print "Fetching data with: POST $req\n" if $debug;
print Dumper $client;
$client->POST($req);
if ( $client->responseCode() == 200 ) {
print "Got Data OK, decoding ... " if $debug;
my $data = $JSON->decode( $client->responseContent() );
print Dumper $data;
return $data;
}
else {
print "Failed with code $client->responseCode()\n";
print Dumper $client->responseContent();
}
}
##
##
rwalk@HeartOfGold~> ./LookupsToWiki.pl -d
Fetching data with: POST /mediawiki/api.php?action=login&lgname=Marvin&lgpassword=leftdiodes&format=json
$VAR1 = bless( {
'_config' => {
'host' => 'http://HeartOfGold.improbable.tea',
'useragent' => bless( {
'protocols_allowed' => undef,
'no_proxy' => [],
'ssl_opts' => {
'verify_hostname' => 1
},
'max_redirect' => 7,
'requests_redirectable' => [
'GET',
'HEAD'
],
'handlers' => {
'response_header' => bless( [
{
'owner' => 'LWP::UserAgent::parse_head',
'line' => '/usr/lib/perl5/vendor_perl/5.18.2/LWP/UserAgent.pm:683',
'm_media_type' => 'html',
'callback' => sub { "DUMMY" }
}
], 'HTTP::Config' )
},
'local_address' => undef,
'proxy' => {},
'show_progress' => undef,
'use_eval' => 1,
'timeout' => 180,
'max_size' => undef,
'def_headers' => bless( {
'user-agent' => 'REST::Client/273'
}, 'HTTP::Headers' ),
'protocols_forbidden' => undef
}, 'LWP::UserAgent' )
},
'_headers' => {
'format' => 'json'
}
}, 'REST::Client' );
Got Data OK, decoding ... $VAR1 = {
'login' => {
'cookieprefix' => 'bitnami_mediawiki',
'sessionid' => 'ltmfbpjfgj5il28d9k5105h8kc2a74db',
'result' => 'NeedToken',
'token' => 'bfbd1a1bdf1d1e0167e07ccdd6bdd4675afdf46d+\\'
},
'warnings' => {
'login' => {
'*' => 'Fetching a token via action=login is deprecated. Use action=query&meta=tokens&type=login instead.'
}
}
};
Got Token: bfbd1a1bdf1d1e0167e07ccdd6bdd4675afdf46d+\
Fetching data with: POST /mediawiki/api.php?action=edit&title=LookupAutomationTest&summary=test%20summary&text=article%20content&format=json
$VAR1 = bless( {
'_config' => {
'host' => 'http://HeartOfGold.improbable.tea',
'useragent' => bless( {
'protocols_allowed' => undef,
'no_proxy' => [],
'ssl_opts' => {
'verify_hostname' => 1
},
'max_redirect' => 7,
'requests_redirectable' => [
'GET',
'HEAD'
],
'handlers' => {
'response_header' => bless( [
{
'owner' => 'LWP::UserAgent::parse_head',
'line' => '/usr/lib/perl5/vendor_perl/5.18.2/LWP/UserAgent.pm:683',
'm_media_type' => 'html',
'callback' => sub { "DUMMY" }
}
], 'HTTP::Config' )
},
'local_address' => undef,
'proxy' => {},
'show_progress' => undef,
'use_eval' => 1,
'timeout' => 300,
'max_size' => undef,
'def_headers' => bless( {
'user-agent' => 'REST::Client/273'
}, 'HTTP::Headers' ),
'protocols_forbidden' => undef
}, 'LWP::UserAgent' )
},
'_headers' => {
'token' => 'bfbd1a1bdf1d1e0167e07ccdd6bdd4675afdf46d+\\',
'format' => 'json'
},
'_res' => bless( {
'_content' => '{"warnings":{"login":{"*":"Fetching a token via action=login is deprecated. Use action=query&meta=tokens&type=login instead."}},"login":{"result":"NeedToken","token":"bfbd1a1bdf1d1e0167e07ccdd6bdd4675afdf46d+\\\\","cookieprefix":"bitnami_mediawiki","sessionid":"ltmfbpjfgj5il28d9k5105h8kc2a74db"}}',
'_protocol' => 'HTTP/1.1',
'_rc' => '200',
'_msg' => 'OK',
'_headers' => bless( {
'content-length' => '295',
'cache-control' => 'private, must-revalidate, max-age=0',
'x-frame-options' => 'DENY',
'::std_case' => {
'x-frame-options' => 'X-Frame-Options',
'client-response-num' => 'Client-Response-Num',
'client-date' => 'Client-Date',
'client-peer' => 'Client-Peer',
'x-powered-by' => 'X-Powered-By',
'x-content-type-options' => 'X-Content-Type-Options',
'set-cookie' => 'Set-Cookie'
},
'date' => 'Thu, 17 May 2018 21:30:21 GMT',
'client-peer' => '10.255.64.83:80',
'x-powered-by' => 'PHP/5.6.21',
'set-cookie' => 'bitnami_mediawiki_session=ltmfbpjfgj5il28d9k5105h8kc2a74db; path=/; httponly',
'connection' => 'close',
'x-content-type-options' => 'nosniff',
'server' => 'Apache/2.4.18 (Unix) OpenSSL/1.0.2h PHP/5.6.21 mod_perl/2.0.8-dev Perl/v5.16.3',
'content-type' => 'application/json; charset=utf-8',
'client-date' => 'Thu, 17 May 2018 21:30:21 GMT',
'client-response-num' => 1
}, 'HTTP::Headers' ),
'_request' => bless( {
'_uri_canonical' => bless( do{\(my $o = 'http://HeartOfGold.improbable.tea/mediawiki/api.php?action=login&lgname=Marvin&lgpassword=leftdiodes&format=json')}, 'URI::http' ),
'_headers' => bless( {
'::std_case' => {
'format' => 'Format'
},
'user-agent' => 'REST::Client/273',
'content-length' => 0,
'format' => 'json'
}, 'HTTP::Headers' ),
'_content' => '',
'_method' => 'POST',
'_uri' => $VAR1->{'_res'}{'_request'}{'_uri_canonical'}
}, 'HTTP::Request' )
}, 'HTTP::Response' )
}, 'REST::Client' );
Got Data OK, decoding ... $VAR1 = {
'error' => {
'code' => 'notoken',
'info' => 'The token parameter must be set',
'*' => 'See http://HeartOfGold.improbable.tea/mediawiki/api.php for API usage'
}
};