Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSRF token expires #181

Open
The-Earth opened this issue Nov 25, 2017 · 5 comments
Open

CSRF token expires #181

The-Earth opened this issue Nov 25, 2017 · 5 comments
Milestone

Comments

@The-Earth
Copy link

The-Earth commented Nov 25, 2017

I have met a problem with the delete method for page object. Here's the code.

It prints error information like this:

Traceback (most recent call last):
  File "clean.py", line 44, in clean
    dpp.delete('Page kept on zhwp.')
  File "/usr/local/lib/python3.5/dist-packages/mwclient/page.py", line 293, in delete
    reason=reason, **data)
  File "/usr/local/lib/python3.5/dist-packages/mwclient/client.py", line 239, in post
    return self.api(action, 'POST', *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/mwclient/client.py", line 282, in api
    if self.handle_api_result(info, sleeper=sleeper):
  File "/usr/local/lib/python3.5/dist-packages/mwclient/client.py", line 323, in handle_api_result
    info['error']['info'], info['error']['*'])
mwclient.errors.APIError: ('badtoken', 'Invalid CSRF token.', 'See https://zhdel.miraheze.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes.')

Sysop flag has already been granted to the bot account. Could anyone tell me what's the problem? Thank you!

@danmichaelo
Copy link
Member

Is it able to delete any pages at all, or do you get this error in the middle of a long run?

Could you try something like this:

from mwclient.errors import APIError

[… rest of your code…]

try:
    dpp.delete('Page kept on zhwp.')
except APIError as e:
    if e.code == 'badtoken':
        print('Got badtoken, retrying')
        dp.tokens = {}   # clear token cache
        dpp.delete('Page kept on zhwp.')    # retry

If this helps, we could add this into mwclient. I've noticed that Page.save() already includes one automatic retry.

@The-Earth
Copy link
Author

The-Earth commented Nov 27, 2017

I usually got this error about 24 hours after it started. I've updated the code and observing its output.

@The-Earth
Copy link
Author

Yes it works! 👍 It once deleted a page successfully shortly after printing "Got badtoken, retrying".

@danmichaelo
Copy link
Member

Good, thanks for testing! Did you notice if it was more or less 24 hours after the start? Not too surprisining if the tokens don't last forever, but I haven't been able to find documentation on it. In any case, it seems like token-refreshing when getting 'badtoken' is something that should be part of mwclient itself, just need to find the best place in the code to handle it (there is already a special handling in the save method).

@The-Earth
Copy link
Author

It's about 18 hours this time. (Maybe shorter. I'm not quite sure about it.)

@danmichaelo danmichaelo changed the title Please help : A problem in Page.delete() CSRF token expires Jun 28, 2018
@danmichaelo danmichaelo added this to the 0.9.1 milestone Jun 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants