Topic on Project:Support desk

Import via API permissions issue?

5
2620:11E:1000:120:3EA9:F4FF:FE85:CA50 (talkcontribs)

I am migrating our old wiki to MediaWiki 1.28.0. To that end, I have exported all of the old wiki pages and am now trying to import them to MediaWiki via the API. But when I try, I get the following response back from the server (in JSON):

{'error': {'code': 'cantimport-upload', '*': 'See http://localhost/api.php for API usage', 'info': "You don't have permission to import uploaded pages"}}

I am trying to do this in Python, but will happily accept solutions or examples using curl.

From an account in the Administrator group, I created a bot password. Here's is what I have so far:

import json
import os

import requests

bot_endpoint = 'http://localhost/api.php'
bot_username = 'Admin@Migration'
bot_password = '<censored>'

session = requests.Session()

with open('/temp/test.xml') as f:
    xml_out = f.read()

# get a login token
login_token_params = {'format':'json', 'action':'query', 'meta':'tokens',
    'type':'login'}
resp = session.get(bot['endpoint'], params=login_token_params)
login_token = json.loads(resp.text)['query']['tokens']['logintoken']

# login to api
login_params = {'format':'json', 'action':'login', 'lgname':bot['username'],
    'lgpassword':bot['password'], 'lgtoken':login_token}
resp = session.post(bot['endpoint'], data=login_params)

# get a csrf token
csrf_token_params = {'format':'json', 'action':'query', 'meta':'tokens',
    'type':'csrf'}
resp = session.get(bot['endpoint'], params=csrf_token_params)
csrf_token = json.loads(resp.text)['query']['tokens']['csrftoken']

# try to upload a file
import_params = {'format':'json', 'action':'import', 'token':csrf_token }
resp = session.post(bot['endpoint'], data=import_params,
    files={'file': ('dummy.xml', xml_out)})


print(json.loads(resp.text))

When I run this, I get the error above. It's possible that I'm not uploading/importing the XML correctly, but good examples of this in python are hard to come by.

I have verified that the user is in the Administrator group (which contains the "importupload" permission). However, when I check the bot in Special:BotPasswords, I don't see any particular grants for importing files. Furthermore, following the link to Special:ListGrants, neither "import" or "importupload" appear there either.

I am certain I am barking up some kind of tree here, just don't know which is the wrong one.

2620:11E:1000:120:3EA9:F4FF:FE85:CA50 (talkcontribs)

ugh, the formating of the code got messed up :(

MarkAHershberger (talkcontribs)

Are you sure you're not getting any errors from the login calls?

2620:11E:1000:120:3EA9:F4FF:FE85:CA50 (talkcontribs)

Yes, fetching the tokens and logging in work just fine, no errors.

2620:11E:1000:120:3EA9:F4FF:FE85:CA50 (talkcontribs)

Also I see some other typos in my script... I copied and pasted a few things wrong, but the original script is working correctly. (Substitute bot['endpoint'] for bot_endpoint, etc.)

Reply to "Import via API permissions issue?"