Page MenuHomePhabricator

MediaWiki API: upload does not work, although all parameters are filled
Closed, ResolvedPublic

Description

Hello to all,

I am writing a tool for Toolforge in Flask, that involves the user being able to upload images to Wikimedia Commons of images of objects similar (like replicas) of objects in a museum collection. I am having difficulties with the use of the API (I've asked here, but with no response).

Basically, I'm receiving the following error when I execute the code below:

  • Code:
client_key = app.config['CONSUMER_KEY']
client_secret = app.config['CONSUMER_SECRET']

session = OAuth1Session(client_key,
                        client_secret=client_secret,
                        resource_owner_key=session['owner_key'],
                        resource_owner_secret=session['owner_secret'])
                        
url = "https://commons.wikimedia.org/w/api.php"# I have used "https://test.wikipedia.org/w/api.php" with the same result

crsf_token = get_token()

params = {
    "action": "upload",
    "filename": <filename>,
    "format": "json",
    "token": csrf_token,
    "ignorewarnings": 1
}

file = {'file': (<filename>, open(<filepath>, 'rb'), 'multipart/form-data')}

response = session.post(url, files=file, data=params)
data = response.json()

return data
  • Error:
{
"error":
  {
    "code":"permissiondenied",
    "docref":"See https://commons.wikimedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes.",
    "info":"The action you have requested is limited to users in one of the groups: *, [[Commons:Users|Users]]."
  },
  "servedby":"mw1278"
}

Is this some kind of bug? Should I use a Bot password instead?

Event Timeline

Ederporto renamed this task from MediaWiki API:Upload to MediaWiki API: upload does not work, although all parameters are filled.May 25 2021, 8:32 PM

@Ederporto did you get this working? My first guess would be that you should check that Oauth-login works (url below) and after this is confirmed then i would try to solve upload. I was stuck with Oauth because I tried to do Oauth1 login with Oauth2 tokens and login failed because that. This could be your error too.

https://commons.wikimedia.org/w/api.php?action=query&meta=userinfo&format=json'

@Ederporto did you get this working? My first guess would be that you should check that Oauth-login works (url below) and after this is confirmed then i would try to solve upload. I was stuck with Oauth because I tried to do Oauth1 login with Oauth2 tokens and login failed because that. This could be your error too.

https://commons.wikimedia.org/w/api.php?action=query&meta=userinfo&format=json'

Me and my bad habits of not closing issues! Yes, I made it work. The problem, I think, it was the request function, I was putting the params as data=params instead of params=params or vice-versa.