Last modified: 2014-06-23 16:07:16 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T54454, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 52454 - Getting invalid token error even after sending the correct edit token while upload image using API
Getting invalid token error even after sending the correct edit token while u...
Status: RESOLVED INVALID
Product: MediaWiki
Classification: Unclassified
API (Other open bugs)
1.21.x
Other Windows 7
: Unprioritized normal (vote)
: ---
Assigned To: Nobody - You can work on this!
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-08-02 13:05 UTC by SenthilNathan
Modified: 2014-06-23 16:07 UTC (History)
4 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description SenthilNathan 2013-08-02 13:05:32 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.
Comment 1 Brad Jorsch 2013-08-02 14:39:52 UTC
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.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links