API:Tokens
Appearance
| Esta página es parte de la documentación de la API de acciones de MediaWiki. |
| Versión de MediaWiki: | ≥ 1.24 |
API:Tokens. El módulo proporciona los tokens requeridos por las acciones de modificación-de-datos, como iniciar sesión, editar o mover una página, y ver o vigilar cambios. Para cada acción, necesitas un tipo concreto de token. Por ejemplo: si deseas iniciar sesión en un sitio wiki vía acción API, necesitarías un token del tipo "iniciar sesión" para proceder.
For help in migrating older code, refer to Deprecation of legacy API token parameters
API Documentación
Ejemplo
CONSIGUE petición
Respuesta
{
"batchcomplete": "",
"query": {
"tokens": {
"logintoken": "9ed1499d99c0c34c73faa07157b3b6075b427365+\\"
}
}
}
Código de muestra
Python
#!/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
<?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 );
// Wikis such as Wikimedia Commons require that you also set a user agent through CURLOPT_USERAGENT
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
echo( $result['query']['tokens']['logintoken'] . "\n" );
Perl
#!/usr/bin/perl
# tokens.pl
# MediaWiki API Demos
# Demo of `Token` module: Fetch token of type `login`
# WTFPL
use strict;
use warnings;
use JSON;
use LWP::UserAgent;
use URI;
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
/*
tokens.js
MediaWiki API Demos
Demo of `Token` module: Fetch token of type `login`
MIT License
*/
var request = require('request'),
url = "https://www.mediawiki.org/w/api.php";
var params = {
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
/*
tokens.js
MediaWiki API Demos
Demo of `Token` module: Fetch token of type `csrf`
MIT License
*/
var params = {
action: 'query',
meta: 'tokens',
type: 'csrf',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
console.log( data.query.tokens.csrftoken );
} );
BASH
export MW_URL="https://www.mediawiki.org/w/"
export API_URL="api.php"
# $0 <token type>
# mainly csrf and login
function get-token(){
API_URL="api.php"
RESULT=$(curl -fsSL -X POST \
-d action=query \
-d meta=tokens \
-d type="$1" \
-d format=json \
-c cookie.txt \
-b cookie.txt \
"${MW_URL}${API_URL}")
RESULT=${RESULT/*token\":\"}
TOKEN=${RESULT%\\\"*}
echo "$TOKEN"
}
TOKEN=$(get-token login)
Posibles errores
| Código | Info |
|---|
Historia de parámetro
- v1.27: Introducido
login,createaccount
Véase también
API:las extensiones#Editan token
- Método obsoleto de obtención de tokens: API:Tokens (action)