Last modified: 2014-06-23 16:07:16 UTC
Hi, I'm trying to edit a page and upload an image using Mediawiki API call. I've logged in as user. I've fetched the edit token using below code: $editToken = urlencode($wgUser->editToken($salt='',$request = $wgRequest)); Below is the post data I'm passing to upload an image using Mediawiki Api, $imgUploadData = "filename=Wiki.png&token=%2B%5C&url=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fen%2Fb%2Fbc%2FWiki.png"; Below is the curl call to execute the api, $submitUrl = "http://example.com/api.php?action=upload&format=json"; $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, $submitUrl); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_POST, 1); curl_setopt($curlObj, CURLOPT_POSTFIELDS, $imgUploadData); $objData = curl_exec($curlObj); curl_close($curlObj); return $objData; When executing the API call its giving the error as below, <api> <error code="badtoken" info="Invalid token" xml:space="preserve"> </error> </api> I'm getting the error as "badtoken" token even after sending the proper edit token. Can you please help me out to sort this issue. I'm struck with this for the past 1 week.
The edit token depends on the proper session cookies, which you are not including in your curl request. But I see larger problems in what you are doing. Apparently this code exists in a MediaWiki extension, as you are calling $wgUser methods to try to get the edit token. If your extension is trying to upload to the local wiki, there's no need to go through the API at all. Just call the appropriate methods as ApiUpload does. If you're trying to upload to a remote wiki, you need to fetch the token from that remote wiki, using $wgUser->editToken() on your local wiki won't work at all.