Jump to content
MediaWiki

API:Tokens

From mediawiki.org
This page is part of the MediaWiki Action API documentation.
MediaWiki Action API
Basics
Authentication
Accounts and Users
Page Operations
Search
Developer Utilities
Tutorials
v · d · e
MediaWiki version:
≥ 1.24

API:Tokens module provide tokens required by data-modifying actions such as logging, editing or moving a page, and watching or patrolling changes. For each action, you need a specific type of token. For example: if you want to login to a wiki site via the Action API, you would need a token of type "login" to proceed.

For help in migrating older code, refer to Deprecation of legacy API token parameters

API documentation

[edit ]
The following documentation is the output of Special:ApiHelp/query+tokens, automatically generated by the pre-release version of MediaWiki that is running on this site (MediaWiki.org).

meta=tokens

(main | query | tokens)

Gets tokens for data-modifying actions.

Specific parameter:
Other general parameters are available.
type

Types of token to request.

Values (separate with | or alternative): createaccount, csrf, deleteglobalaccount, login, patrol, rollback, setglobalaccountstatus, userrights, watch
To specify all values, use *.
Default: csrf
Examples:
Retrieve a csrf token (the default).
api.php?action=query&meta=tokens [open in sandbox]
Retrieve a watch token and a patrol token.
api.php?action=query&meta=tokens&type=watch|patrol [open in sandbox]


Example

[edit ]

GET request

[edit ]

Response

[edit ]
{
"batchcomplete":"",
"query":{
"tokens":{
"logintoken":"9ed1499d99c0c34c73faa07157b3b6075b427365+\\"
}
}
}

Sample code

[edit ]

Python

[edit ]
#!/usr/bin/python3
"""
 tokens.py
 MediaWiki API Demos
 Demo of `Token` module: Fetch token of type `login`
 MIT License
"""
import requests
S = requests.Session()
URL = "https://www.mediawiki.org/w/api.php"
PARAMS = {
 "action": "query",
 "meta": "tokens",
 "type": "login",
 "format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
print(LOGIN_TOKEN)

PHP

[edit ]
<?php
/*
 tokens.php
 MediaWiki API Demos
 Demo of `Token` module: Fetch token of type `login`
 MIT License
*/
$endPoint = "https://www.mediawiki.org/w/api.php";
$params = [
 "action" => "query",
 "meta" => "tokens",
 "type" => "login",
 "format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
echo( $result['query']['tokens']['logintoken'] . "\n" );

Perl

[edit ]
#!/usr/bin/perl
# token.pl
# MediaWiki API Demos
# Demo of `Token` module: Fetch token of type `login`
# WTFPL
usestrict;
usewarnings;
useJSON;
useLWP::UserAgent;
useURI;
my$url=URI->new('https://www.mediawiki.org/w/api.php');
my%params=(
'action'=>'query',
'meta'=>'tokens',
'type'=>'login',
'format'=>'json',
);
$url->query_form(%params);
my$ua=LWP::UserAgent->new();
my$response=$ua->get($url);
my$data=JSON::decode_json($response->content);
my$login_token=$data->{'query'}{'tokens'}{'logintoken'};
print"$login_token\n";

Node.js

[edit ]
/*
 tokens.js
 MediaWiki API Demos
 Demo of `Token` module: Fetch token of type `login`
 MIT License
*/
varrequest=require('request'),
url="https://www.mediawiki.org/w/api.php";
varparams={
action:"query",
meta:"tokens",
type:"login",
format:"json"
};
request.get({url:url,qs:params},function(error,response,body){
body=JSON.parse(body);
console.log(body.query.tokens.logintoken);
});

MediaWiki JS

[edit ]
/*
 tokens.js
 MediaWiki API Demos
 Demo of `Token` module: Fetch token of type `csrf`
 MIT License
*/
varparams={
action:'query',
meta:'tokens',
type:'csrf',
format:'json'
},
api=newmw.Api();
api.get(params).done(function(data){
console.log(data.query.tokens.csrftoken);
});

Possible errors

[edit ]
Code Info

Parameter history

[edit ]
  • v1.27: Introduced login, createaccount

See also

[edit ]

AltStyle によって変換されたページ (->オリジナル) /