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!

In reply to Media Wiki API 'WrongToken' by Random_Walk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.