Page MenuHomePhabricator

page.coordinates() fails in pywikibot
Open, Needs TriagePublicBUG REPORT

Description

import pywikibot
asite = pywikibot.Site('de')
#asite.login()
page = pywikibot.Page(asite, "Wien")
print (page.title()) # works
print (page.coordinates(primary_only=True)) # fails with 
# WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here
# NoUsernameError: Failed OAuth authentication for wikipedia:test: The authorization headers in your request are for a user that does not exist here

why can I access some properties (like title()) and get an error when accessing coordinates()?
With my config I can access wikipedia, commons, wikidata without problems, but coordinates() fails.

full stacktrace:

---------------------------------------------------------------------------
NoUsernameError                           Traceback (most recent call last)
Cell In[60], line 6
      4 page = pywikibot.Page(asite, "Wien")
      5 print (page.title()) # works
----> 6 print (page.coordinates(primary_only=True)) # fails with 
      7 # WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here
      8 # NoUsernameError: Failed OAuth authentication for wikipedia:test: The authorization headers in your request are for a user that does not exist here
      9 
   (...)
     13 #api.loadpageinfo(page)
     14 #page.loadpageprops()

File /srv/paws/lib/python3.10/site-packages/pywikibot/page/_basepage.py:1673, in BasePage.coordinates(self, primary_only)
   1671 if not hasattr(self, '_coords'):
   1672     self._coords = []
-> 1673     self.site.loadcoordinfo(self)
   1674 if primary_only:
   1675     for coord in self._coords:

File /srv/paws/lib/python3.10/site-packages/pywikibot/site/_decorators.py:58, in need_extension.<locals>.decorator.<locals>.callee(self, *args, **kwargs)
     54 if not self.has_extension(extension):
     55     raise UnknownExtensionError(
     56         'Method "{}" is not implemented without the extension {}'
     57         .format(fn.__name__, extension))
---> 58 return fn(self, *args, **kwargs)

File /srv/paws/lib/python3.10/site-packages/pywikibot/site/_extensions.py:183, in GeoDataMixin.loadcoordinfo(self, page)
    175 title = page.title(with_section=False)
    176 query = self._generator(api.PropertyGenerator,
    177                         type_arg='coordinates',
    178                         titles=title.encode(self.encoding()),
   (...)
    181                                 'globe'],
    182                         coprimary='all')
--> 183 self._update_page(page, query)

File /srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py:1438, in APISite._update_page(self, page, query, verify_imageinfo)
   1435 if not self.sametitle(pageitem['title'],
   1436                       page.title(with_section=False)):
   1437     raise InconsistentTitleError(page, pageitem['title'])
-> 1438 api.update_page(page, pageitem, query.props)
   1440 if verify_imageinfo and 'imageinfo' not in pageitem:
   1441     if 'missing' in pageitem:

File /srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_generators.py:1021, in update_page(page, pagedict, props)
   1018     page._langlinks = set()
   1020 if 'coordinates' in pagedict:
-> 1021     _update_coordinates(page, pagedict['coordinates'])
   1023 if 'pageimage' in pagedict:
   1024     page._pageimage = pywikibot.FilePage(page.site, pagedict['pageimage'])

File /srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_generators.py:950, in _update_coordinates(page, coordinates)
    948 coords = []
    949 for co in coordinates:
--> 950     coord = pywikibot.Coordinate(lat=co['lat'],
    951                                  lon=co['lon'],
    952                                  typ=co.get('type', ''),
    953                                  name=co.get('name', ''),
    954                                  dim=int(co.get('dim', 0)) or None,
    955                                  globe=co['globe'],  # See [[gerrit:67886]]
    956                                  primary='primary' in co
    957                                  )
    958     coords.append(coord)
    959 page._coords = coords

File /srv/paws/lib/python3.10/site-packages/pywikibot/_wbtypes.py:129, in Coordinate.__init__(self, lat, lon, alt, precision, globe, typ, name, dim, site, globe_item, primary)
    127 self.name = name
    128 self._dim = dim
--> 129 self.site = site or pywikibot.Site().data_repository()
    130 self.primary = primary
    132 if globe:

File /srv/paws/lib/python3.10/site-packages/pywikibot/__init__.py:243, in Site(code, fam, user, interface, url)
    241 key = f'{interface.__name__}:{fam}:{code}:{user}'
    242 if key not in _sites or not isinstance(_sites[key], interface):
--> 243     _sites[key] = interface(code=code, fam=fam, user=user)
    244     debug(f"Instantiated {interface.__name__} object '{_sites[key]}'")
    246     if _sites[key].code != code:

File /srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py:140, in APISite.__init__(self, code, fam, user)
    138 self._loginstatus = login.LoginStatus.NOT_ATTEMPTED
    139 with suppress(SiteDefinitionError):
--> 140     self.login(cookie_only=True)

File /srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py:400, in APISite.login(self, autocreate, user, cookie_only)
    398 try:
    399     del self.userinfo  # force reload
--> 400     if self.userinfo['name'] == self.user():
    401         self._loginstatus = login.LoginStatus.AS_USER
    402         return

File /srv/paws/lib/python3.10/site-packages/pywikibot/site/_apisite.py:668, in APISite.userinfo(self)
    661 if not hasattr(self, '_userinfo'):
    662     uirequest = self.simple_request(
    663         action='query',
    664         meta='userinfo',
    665         uiprop='blockinfo|hasmsg|groups|rights|ratelimits',
    666         formatversion=2,
    667     )
--> 668     uidata = uirequest.submit()
    669     assert 'query' in uidata, \
    670            "API userinfo response lacks 'query' key"
    671     assert 'userinfo' in uidata['query'], \
    672            "API userinfo response lacks 'userinfo' key"

File /srv/paws/lib/python3.10/site-packages/pywikibot/data/api/_requests.py:1087, in Request.submit(self)
   1085         pywikibot.error(f'Retrying failed {msg}')
   1086         continue
-> 1087     raise NoUsernameError(f'Failed {msg}')
   1089 if code == 'cirrussearch-too-busy-error':  # T170647
   1090     self.wait()

NoUsernameError: Failed OAuth authentication for wikipedia:test: The authorization headers in your request are for a user that does not exist here

Event Timeline

matej_suchanek subscribed.

Your script is apparently making a request to test.wikipedia.org and fails, claiming you don't have an account there. It's not clear to me why.
Assuming you are using Herzi Pinki account (are you?), this is not true.

Would you please post more information about your environment (e.g., the output of pwb.py version)?

pwb.py version:

Pywikibot: [https] r-pywikibot-core.git (60e9ec2, g1, 2024/03/08, 07:47:53, OUTDATED)
Release version: 9.0.0
packaging version: 23.2
mwparserfromhell version: 0.6.6
wikitextparser version: n/a
requests version: 2.31.0
  cacerts: /srv/paws/lib/python3.10/site-packages/certifi/cacert.pem
    certificate test: ok
Python: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
PYWIKIBOT_DIR: /srv/paws
PYWIKIBOT_DIR_PWB: /srv/paws/pwb/pywikibot/scripts
PYWIKIBOT_NO_USER_CONFIG: Not set
PYWIKIBOT_VERSION: 9.0.0
Config base dir: /srv/paws
Usernames for family 'wikipedia':
        *: Herzi Bot Pinki
Usernames for family 'commons':
        *: Herzi Bot Pinki
Usernames for family 'meta':
        *: Herzi Bot Pinki
Usernames for family 'wikibooks':
        *: Herzi Bot Pinki
Usernames for family 'wikimedia':
        *: Herzi Bot Pinki
Usernames for family 'wikiquote':
        *: Herzi Bot Pinki
Usernames for family 'wikisource':
        *: Herzi Bot Pinki
Usernames for family 'wiktionary':
        *: Herzi Bot Pinki
Usernames for family 'wikiversity':
        *: Herzi Bot Pinki
Usernames for family 'wikidata':
        *: Herzi Bot Pinki
Usernames for family 'mediawiki':
        *: Herzi Bot Pinki
Usernames for family 'wikivoyage':
        *: Herzi Bot Pinki
Usernames for family 'wikinews':
        *: Herzi Bot Pinki

and

cat .my.cnf
[client]
password = xxxxxxxxxx
user = p66218851

It is my Bot, Herzi Bot Pinki

I've seen the test, but that is not what I want to access, but to get coordinates from dewiki

That explains the "user that does not exist here". The bot is not registered on test.wikipedia.
If you log in to the bot account and visit a page on test.wikipedia.org, I believe it should solve the problem for you.

But still, it isn't clear why it makes a request to test.wikipedia at all...

Now I logged in to test.wikipedia.org, it does not change the behavior.
I also tried to log out and log in from paws, did not change anything.
thanks for caring - still help needed

(and yes, why the test.wikipedia is involved, is a miracle ...