User:Patrick Nagel/Login with snoopy post-1.15.3

API:Login describes how to log in to a Wiki using the API. This was easy to do with Snoopy in the days before MediaWiki 1.15.3 - I used the code from MediaWiki_Bulk_Page_Creator. Since this is no longer possible (for a good reason: security), here is the code I use now:

curl_path="/usr/bin/curl"; $wikiroot = "https://my.private.wiki/wiki"; $api_url = $wikiroot. "/api.php";

$login_vars['action'] = "login"; $login_vars['lgname'] = "botusername"; $login_vars['lgpassword'] = "botpassword"; $login_vars['format'] = "php"; $snoopy->submit($api_url,$login_vars); $response = unserialize($snoopy->results); $login_vars['lgtoken'] = $response[login][token]; $snoopy->cookies["wiki_session"] = $response[login][sessionid]; // You may have to change 'wiki_session' to something else on your Wiki $snoopy->submit($api_url,$login_vars);
 * 1) Login via api.php
 * 1) First part
 * 1) Second part, now that we have the token

$login_vars['action'] = "render"; $urlpart=$argv[1]; $snoopy->submit($wikiroot . "/index.php?title=" . $urlpart, $login_vars); print($snoopy->results);
 * 1) Fetch the page

(it actually fetches a page from a private Wiki, with action=render - but after having logged in, you can do whatever you want)

Note that I'm not very experienced with neither Snoopy, nor the MediaWiki API, yet, so it may well be that here is a better solution. Feel free to point out improvements on the discussion page! --Patrick Nagel 03:22, 30 June 2010 (UTC)

An anonymous user points out on the discussion page, that the script can be made independent of the cookie name. --Patrick Nagel 02:36, 23 July 2010 (UTC)