Topic on Project:Support desk

API Question - Create/Editing page

3
75.68.66.120 (talkcontribs)

Hello,

I am having some trouble with the API and my MediaWIKI website.

My goal is to create a Perl script for my college capstone class that allows the user to create a new page on the wiki when they are giving input to the script. I won't go into detail as to why they have to create a new page, the page just has to be created when the user is running this specific Perl script and instead of just telling them as an instruction on the script, it would be nice if when they give input through the script so I can programmatically create the page.

Anyways, here is what I have so far: - I got my API token by going to my API link: api.php?action=tokens

Here is the code I'm currently using for the script:

my $browser = new LWP::UserAgent();
$browser->agent('Mozilla/5.0');
$browser->default_header('Accept-Encoding' => scalar HTTP::Message::decodable());
$browser->default_header('Accept-Language' => "no, en");
$browser->default_header('Content_Type' => 'application/x-www-form-urlencoded');
my $api_url = "http://<mywebsite>/api.php";
my $request = HTTP::Request->new(POST => $api_url);
$request->header('Accept-Encoding' => scalar HTTP::Message::decodable());
$request->header('Accept-Language' => "no, en");
$request->header('Content_Type' => 'application/x-www-form-urlencoded');

# Add our data.
# This token is required for edit requests - must also be added last.
my $api_edit_token = "6bde13537ae386fd6a72a232bd03cbbf+\\";
my $content_title = "Speech:TestingAPIWithPerlAPI";
my $content_text = "This is my second time trying our the Perl API.";
my $post_data = 'action=edit&format=json&title='. $content_title .'&text='. $content_text .'&token=' . $api_edit_token;

$request->content($post_data);

# Fire away.
my $response = $browser->request($request);
if ($response->is_success) {
    my $message = $response->decoded_content;
    print "Received reply:\n$message\n";
}
else {
    print "HTTP POST error code: ", $response->code, "\n";
    print "HTTP POST error message: ", $response->message, "\n";
}

Now this runs perfectly fine, however I keep getting the same error over and over: {"error":{"code":"badtoken","info":"Invalid token"}}

Obviously I'm making sure that my token is correct and up to date as I keep checking the token api link. Before the POST request is actually made, I print out the request as a string:

action=edit&format=json&title=Speech:TestingAPIWithPerlAPI&text=This is my second time trying our the Perl API.&token=6bde13537ae386fd6a72a232bd03cbbf+\

Everything seems fine there!

Anyone run into this? I have a feeling it has to do with the urlencoding of the +\ in the token... thanks!

Ciencia Al Poder (talkcontribs)

The token can be different for every page, and will be different for every user.

You should retrieve the token for that page first, and then post the content with that token, as explained in API:Edit#Token.

If you're logging in with that script, be sure to receive and send also the cookies!

Ricordisamoa (talkcontribs)
Reply to "API Question - Create/Editing page"