API:Logout

From MediaWiki.org
Jump to navigation Jump to search

Other languages:
Deutsch • ‎English • ‎dansk • ‎español • ‎français • ‎lingála • ‎polski • ‎português do Brasil • ‎română • ‎русский • ‎العربية • ‎ไทย • ‎中文 • ‎日本語 • ‎한국어
MediaWiki version: 1.12

GET request to log out of account.

Logging out deletes the log in tokens and other browser cookies.

API documentation[edit]


action=logout

(main | logout)
  • This module only accepts POST requests.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Log out and clear session data.

Parameter:
token

A "csrf" token retrieved from action=query&meta=tokens

This parameter is required.
Example:
Log the current user out.
api.php?action=logout&token=123ABC [open in sandbox]

Example[edit]

GET request[edit]

Response[edit]

{

}

Sample code[edit]

logout.py

#!/usr/bin/python3

"""
    logout.py

    MediaWiki API Demos
    Demo of `Logout` module: Log out and clear session data.

    MIT License
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

# Step 1: Retrieve login token first
PARAMS_0 = {
    'action':"query",
    'meta':"tokens",
    'type':"login",
    'format':"json"
}

R = S.get(url=URL, params=PARAMS_0)
DATA = R.json()

LOGIN_TOKEN = DATA['query']['tokens']['logintoken']

# Step 2: Send a POST request to login. Using the main account for login is not
# supported. Obtain credentials via Special:BotPasswords
# (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword

PARAMS_1 = {
    'action':"login",
    'lgname':"your_bot_username",
    'lgpassword':"your_bot_password",
    'lgtoken':LOGIN_TOKEN,
    'format':"json"
}

R = S.post(URL, data=PARAMS_1)
DATA = R.json()

# Step 3: GET request to fetch CSRF token
PARAMS_2 = {
    "action":"query",
    "meta":"tokens",
    "format":"json"
}

R = S.get(url=URL, params=PARAMS_2)
DATA = R.json()

CSRF_TOKEN = DATA['query']['tokens']['csrftoken']

# Step 4: Send a POST request to logout
PARAMS_3 = {
    "action": "logout",
    "token": CSRF_TOKEN,
    "format": "json"
}

R = S.post(URL, data=PARAMS_3)
DATA = R.json()

print(DATA)

Additional notes[edit]

See also[edit]