Wikimedia Developer Support

Can't login with pywikibot

I’ve managed to badly edit 30,000 pages on my wiki using the ReplaceText extension (Great extension, poor testing of my regex.)

I’ve used pywikibot before multiple times, and I figured this would be the best way to revert my edits. There is already a ‘revertbot’ script written. I’ve followed instructions for 3rd-party wikis and I’ve setup a bot password at Special:BotPasswords. I’ve tried logging in with my regular user account (full bot, bureaucrat, sysadmin etc.) as well as using the special bot account under my username (user@QualityBot). No matter what combination of username and password I’ve tried, I am unable to login with Pywikibot. I’ve tried setting up Pywikibot on 4 separate wikis/environments. I’ve tried running it with the deprecated Python 2.7 and with the supported Python 3.5

Here is my configuration (python pwb.py version), and debug output of a login attempt. I get ‘The supplied credentials could not be authenticated.’

I also see an error line that says: ‘pywikibot.exceptions.NoUsername: Username “Admin” does not have read permissions on freephile:en’ but the wikis I’m trying to login to are world-readable.

I guess if I can’t get this to work, I can always write my own script to use the Rollback API, but I’m stumped as to why I can’t login with pywikibot.

After ensuring that
wgEnableWriteAPI = true; and
$wgGroupPermissions['*']['writeapi'] = true;
writeapi permssion is granted to all in LocalSettings.php, I am able to login using the API, but still not with pywikibot.

root@fswiki:~/bin/pywikibot# python3 pwb.py login -v -debug 
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516]
handle_args() completed.
Instantiated APISite object 'fsw:en'
general siteinfo not loaded yet.
general siteinfo not loaded yet.
Load siteinfo properties 'namespaces', 'namespacealiases' along with 'general'
Load siteinfo properties 'namespaces', 'namespacealiases' along with 'general'
CachedRequest: cache hit (/root/bin/pywikibot/apicache-py3/fd5ca8862026ab8e17cd31ba03aa16417a3da265c55fd20108941c58869d4df8) for API request: APISite("en", "fsw")LoginStatus(-1)[('action', 'query'), ('continue', ''), ('format', 'json'), ('maxlag', '5'), ('meta', 'siteinfo|userinfo'), ('siprop', 'namespaces|namespacealiases|general'), ('uiprop', 'blockinfo|hasmsg')]
Checking multiplicity: pid = False
Checking multiplicity: pid = False
Found 1 fsw:en processes running, including this one.
API request to fsw:en (uses get: True):
Headers: {'Content-Type': 'application/x-www-form-urlencoded'}
URI: '/wiki/en/api.php?action=query&format=json&meta=userinfo&maxlag=5&rawcontinue=&uiprop=blockinfo%7Cgroups%7Chasmsg%7Crights'
Body: None
API response received from fsw:en:
{"query":{"userinfo":{"id":0,"name":"50.16.129.198","anon":"","groups":["*"],"rights":["createaccount","read","writeapi","viewmywatchlist","editmywatchlist","viewmyprivateinfo","editmyprivateinfo","editmyoptions","autocreateaccount","viewedittab"]}}}
WARNING: No user is logged in on site fsw:en
WARNING: /root/bin/pywikibot/pywikibot/login.py:118: UserWarning: When using BotPasswords it is recommended that you store your login credentials in a password_file instead. See https://www.mediawiki.org/wiki/Manual:Pywikibot/BotPasswords for instructions and more information.
  .format(__url__))

CachedRequest: cache hit (/root/bin/pywikibot/apicache-py3/df9d89dca18b7e1a75e83c42c1962b07ace2b21c7ad2e7271dd2b6bd44113501) for API request: APISite("en", "fsw")LoginStatus(-1)[('action', 'paraminfo'), ('format', 'json'), ('maxlag', '5'), ('modules', 'main|paraminfo|query')]
CachedRequest: cache hit (/root/bin/pywikibot/apicache-py3/050fae61775b092e00894fb984453e58bdf13be584b39541a90eda19d7afa5f7) for API request: APISite("en", "fsw")LoginStatus(-1)[('action', 'paraminfo'), ('format', 'json'), ('maxlag', '5'), ('modules', 'query+allusers')]
ListGenerator: Set query_limit to 500.
ListGenerator: query_limit: 500, api_limit: 500, limit: 1, new_limit: 1, count: 0
ListGenerator: aulimit: ['1']
API request to fsw:en (uses get: True):
Headers: {'Content-Type': 'application/x-www-form-urlencoded'}
URI: '/wiki/en/api.php?meta=userinfo&aulimit=1&list=allusers&auprop=editcount%7Cgroups%7Cregistration&continue=&maxlag=5&format=json&action=query&aufrom=Freephile&uiprop=blockinfo%7Chasmsg&indexpageids='
Body: None
API response received from fsw:en:
{"batchcomplete":"","continue":{"aufrom":"Freepons","continue":"-||userinfo"},"query":{"allusers":[{"userid":2356968,"name":"Freephile","editcount":19646,"registration":"2018-04-12T01:04:15Z","groups":["*","user","autoconfirmed","Moderator","Reviewer","bot","bureaucrat","editor","interface-admin","pagecreator","suppress","sysop","widgeteditor"]}],"userinfo":{"id":0,"name":"50.16.129.198","anon":""}}}
ListGenerator received [{'name': 'Freephile', 'editcount': 19646, 'registration': '2018-04-12T01:04:15Z', 'groups': ['*', 'user', 'autoconfirmed', 'Moderator', 'Reviewer', 'bot', 'bureaucrat', 'editor', 'interface-admin', 'pagecreator', 'suppress', 'sysop', 'widgeteditor'], 'userid': 2356968}]; limit=1
Password for user Freephile@QualityBot on fsw:en (no characters will be shown): 
Logging in to fsw:en as Freephile@QualityBot
API request to fsw:en (uses get: False):
Headers: {'Content-Type': 'application/x-www-form-urlencoded'}
URI: '/wiki/en/api.php'
Body: 'action=query&format=json&meta=tokens&maxlag=5&type=login&rawcontinue='
API response received from fsw:en:
{"query":{"tokens":{"logintoken":"d92cf07452788d1e2739939ddb1a8e255e57d336+\\"}}}
API request to fsw:en (uses get: False):
Headers: {'Content-Type': 'application/x-www-form-urlencoded'}
URI: '/wiki/en/api.php'
Body: 'action=login&format=json&maxlag=5&lgpassword=REDACTED&lgname=Freephile%40QualityBot&lgtoken=d92cf07452788d1e2739939ddb1a8e255e57d336%2B%5C'
API response received from fsw:en:
{"login":{"result":"Failed","reason":"The supplied credentials could not be authenticated."}}
ERROR: Login failed (Failed).
Traceback (most recent call last):
  File "/root/bin/pywikibot/pywikibot/login.py", line 299, in login
    cookiedata = self.getCookie()
  File "/root/bin/pywikibot/pywikibot/tools/__init__.py", line 1873, in wrapper
    return obj(*new_args, **new_kwargs)
  File "/root/bin/pywikibot/pywikibot/data/api.py", line 3156, in getCookie
    raise APIError(code=login_result['login']['result'], info=info)
pywikibot.data.api.APIError: Failed: The supplied credentials could not be authenticated.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pwb.py", line 325, in <module>
    if not main():
  File "pwb.py", line 320, in main
    run_python_file(filename, [filename] + args, argvu, file_package)
  File "pwb.py", line 101, in run_python_file
    main_mod.__dict__)
  File "./scripts/login.py", line 188, in <module>
    main()
  File "./scripts/login.py", line 170, in main
    site.login(autocreate=autocreate)
  File "/root/bin/pywikibot/pywikibot/tools/__init__.py", line 1790, in wrapper
    return obj(*__args, **__kw)
  File "/root/bin/pywikibot/pywikibot/site.py", line 2100, in login
    if login_manager.login(retry=True, autocreate=autocreate):
  File "/root/bin/pywikibot/pywikibot/login.py", line 308, in login
    raise NoUsername(error_msg)
pywikibot.exceptions.NoUsername: Username "Freephile@QualityBot" does not have read permissions on fsw:en
.The supplied credentials could not be authenticated.
_flush() called
Dropped throttle(s).
Closing network session.
CRITICAL: Exiting due to uncaught exception <class 'pywikibot.exceptions.NoUsername'>
Network session closed.

Are you providing the password on a configuration file?

That’s what I have:

On user-config.py I have:

family = 'mywiki'
usernames['mywiki']['en'] = u'BotUser'
password_file = "user-password.py"

Then, in the user-password.py file:

(u'en', u'mywiki', u'BotUser', BotPassword(u'botname', u'botpassword'))

To get the bot password I go to Special:BotPasswords on the wiki I want to edit.

Body: 'action=login&format=json&maxlag=5&lgpassword=REDACTED&lgname=Freephile%40QualityBot&lgtoken=d92cf07452788d1e2739939ddb1a8e255e57d336%2B%5C'
API response received from fsw:en:
{"login":{"result":"Failed","reason":"The supplied credentials could not be authenticated."}}

That’s a straightforward wrong password error. Maybe the bot password was set up for a different account or you made a typo.