jenkins-bot has submitted this change and it was merged.
Change subject: Get login token from action=query&meta=tokens ......................................................................
Get login token from action=query&meta=tokens
Tries to get the login token from action=query&meta=tokens and falls back on looking for the NeedToken response to the login call.
Bug: T127205 Change-Id: I5e46fddf28c562091347aad3c4c5f0177c331a45 --- M pywikibot/data/api.py 1 file changed, 27 insertions(+), 0 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index 0881e62..a42caf6 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -2993,11 +2993,18 @@ pywikibot.warning(u"Too many tries, waiting %s seconds before retrying." % diff.seconds) time.sleep(diff.seconds) + + # base login request login_request = self.site._request( use_get=False, parameters=dict(action='login', lgname=self.username, lgpassword=self.password)) + + # get token using meta=tokens if supported + if MediaWikiVersion(self.site.version()) >= MediaWikiVersion('1.27'): + login_request["lgtoken"] = self.get_login_token() + self.site._loginstatus = -2 while True: login_result = login_request.submit() @@ -3013,6 +3020,7 @@ self.username = login_result['login']['lgusername'] return "\n".join(cookies) elif login_result['login']['result'] == "NeedToken": + # Kept for backwards compatibility token = login_result['login']['token'] login_request["lgtoken"] = token continue @@ -3028,6 +3036,25 @@ """Ignore data; cookies are set by threadedhttp module.""" http.cookie_jar.save()
+ def get_login_token(self): + """Fetch login token from action=query&meta=tokens. + + Requires MediaWiki >= 1.27. + + @return: login token + @rtype: str + """ + if MediaWikiVersion(self.site.version()) < MediaWikiVersion('1.27'): + raise NotImplementedError('The method get_login_token() requires ' + 'at least MediaWiki version 1.27.') + login_token_request = self.site._request( + use_get=False, + parameters=dict(action='query', + meta='tokens', + type='login')) + login_token_result = login_token_request.submit() + return login_token_result['query']['tokens'].get('logintoken') +
def encode_url(query): """
pywikibot-commits@lists.wikimedia.org