Page MenuHomePhabricator

Pywikibot commonscat script can't handle OAuth
Open, Needs TriagePublicBUG REPORT

Description

After following https://www.mediawiki.org/wiki/Manual:Pywikibot/OAuth/Wikimedia:
On Toolforge, run:

tools.arambot@tools-sgebastion-10:~$ $HOME/pwbvenv/bin/python3 $HOME/pywikibot-core/pwb.py commonscat.py -family:wikipedia -lang:ckb -ns:0 -ns:14 -recentchanges

And as we see, it works a little bit (without any actions), but throws these error then:

Retrieving 50 pages from wikipedia:ckb.

>>> پۆل:ڕەقەکان <<<
Commonscat template is already on پۆل:ڕەقەکان
WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here

1 read operation
Execution time: 1 seconds
Read operation time: 1.0 seconds
Script terminated by exception:

ERROR: Failed OAuth authentication for commons:commons: The authorization headers in your request are for a user that does not exist here (NoUsernameError)
Traceback (most recent call last):
  File "/data/project/arambot/pywikibot-core/pwb.py", line 39, in <module>
    sys.exit(main())
  File "/data/project/arambot/pywikibot-core/pwb.py", line 35, in main
    runpy.run_path(str(path), run_name='__main__')
  File "/usr/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 513, in <module>
    main()
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 497, in main
    if not execute():
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 484, in execute
    run_python_file(filename, script_args, module)
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 148, in run_python_file
    main_mod.__dict__)
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 576, in <module>
    main()
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 570, in main
    bot.run()
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/bot.py", line 1664, in run
    self.treat(page)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/bot.py", line 1917, in treat
    self.treat_page()
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 296, in treat_page
    currentCommonscatTarget)
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 469, in checkCommonscatLink
    commonsSite = self.current_page.site.image_repository()
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 1170, in image_repository
    return pywikibot.Site(code, fam, self.username())
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/__init__.py", line 266, in Site
    _sites[key] = interface(code=code, fam=fam, user=user)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_datasite.py", line 38, in __init__
    super().__init__(*args, **kwargs)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 127, in __init__
    self.login(cookie_only=True)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 396, in login
    raise e
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 387, in login
    if self.userinfo['name'] == self.user():
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 553, in userinfo
    uidata = uirequest.submit()
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/data/api/_requests.py", line 1070, in submit
    raise NoUsernameError(f'Failed {msg}')
pywikibot.exceptions.NoUsernameError: Failed OAuth authentication for commons:commons: The authorization headers in your request are for a user that does not exist here
CRITICAL: Exiting due to uncaught exception NoUsernameError: Failed OAuth authentication for commons:commons: The authorization headers in your request are for a user that does not exist here

We don't take any action on https://commons.wikimedia.org using commonscat.py script and why we got these errors? I still added usernames['commons']['commons'] = 'AramBot', but got the same errors again.

Event Timeline

JJMC89 subscribed.

The script queries the Commons API, which requires the account to exist there, but it doesn't.

Sorry for reopening. You were right. My bot wasn't attached to Commons, but after attaching, I still faces these errors. Even I can't understand it; it's weird!

ERROR: No username has been defined in your user config file: you have to add in this file the following line:
usernames[Family("wikipedia")]['en']= 'AramBot' (NoUsernameError)
Traceback (most recent call last):
  File "/data/project/arambot/pywikibot-core/pwb.py", line 39, in <module>
    sys.exit(main())
  File "/data/project/arambot/pywikibot-core/pwb.py", line 35, in main
    runpy.run_path(str(path), run_name='__main__')
  File "/usr/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 513, in <module>
    main()
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 497, in main
    if not execute():
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 484, in execute
    run_python_file(filename, script_args, module)
  File "/data/project/arambot/pywikibot-core/pywikibot/scripts/wrapper.py", line 148, in run_python_file
    main_mod.__dict__)
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 576, in <module>
    main()
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 570, in main
    bot.run()
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/bot.py", line 1664, in run
    self.treat(page)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/bot.py", line 1917, in treat
    self.treat_page()
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 324, in treat_page
    commonscatLink = self.find_commons_category(page)
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 427, in find_commons_category
    return self.findCommonscatLink(page)
  File "/data/project/arambot/pywikibot-core/scripts/commonscat.py", line 383, in findCommonscatLink
    for ipageLink in page.langlinks():
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/page/_basepage.py", line 1523, in langlinks
    self._langlinks = set(self.iterlanglinks(include_obsolete=True))
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_generators.py", line 816, in pagelanglinks
    source=self)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/page/_links.py", line 605, in langlinkUnsafe
    link._site = pywikibot.Site(lang, source.family.name)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/__init__.py", line 266, in Site
    _sites[key] = interface(code=code, fam=fam, user=user)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 127, in __init__
    self.login(cookie_only=True)
  File "/mnt/nfs/labstore-secondary-tools-project/arambot/pywikibot-core/pywikibot/site/_apisite.py", line 416, in login
    raise NoUsernameError(error_msg)
pywikibot.exceptions.NoUsernameError: No username has been defined in your user config file: you have to add in this file the following line:
usernames[Family("wikipedia")]['en']= 'AramBot'
CRITICAL: Exiting due to uncaught exception NoUsernameError: No username has been defined in your user config file: you have to add in this file the following line:
usernames[Family("wikipedia")]['en']= 'AramBot'

Change 998295 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [bugfix] use str instead of repr for family

https://gerrit.wikimedia.org/r/998295

Change 998295 merged by jenkins-bot:

[pywikibot/core@master] [bugfix] use str instead of repr for family

https://gerrit.wikimedia.org/r/998295

@Aram: what about the suggested config setting? Does this help:

usernames['wikipedia']['en'] = 'AramBot'

@Xqt, No. Every time it asks to add a new project.

usernames['wikipedia']['en'] = 'AramBot'
usernames['wikipedia']['fa']= 'AramBot'
usernames['wikipedia']['id']= 'AramBot'
usernames['wikipedia']['am']= 'AramBot'
...

@Xqt, No. Every time it asks to add a new project.

usernames['wikipedia']['en'] = 'AramBot'
usernames['wikipedia']['fa']= 'AramBot'
...

Hm, there are several sites in langlinks but it should not have to logged in there.
For a work-around you could use
usernames['wikipedia']['*'] = 'AramBot'

Hm, there are several sites in langlinks but it should not have to logged in there.
For a work-around you could use
usernames['wikipedia']['*'] = 'AramBot'

@Xqt, This time it worked without any errors! I don’t really know why we have to specify all the projects. For me to use "commonscat.py" script with OAuth, I have to enter these two lines.

usernames['commons']['commons'] = 'AramBot'
usernames['wikipedia']['*'] = 'AramBot'

Previously and without OAuth the script worked without entering those two lines. Thanks!