Jump to content

API:令牌

本頁使用了標題或全文手工轉換
From mediawiki.org
This page is a translated version of the page API:Tokens and the translation is 100% complete.
MediaWiki版本:
1.24

API:Tokens模块提供数据修改操作所需的令牌,例如记录、编辑或移动页面,以及观察或巡查更改。

对于每个操作,您需要特定类型的令牌。 例如:如果您想通过Action API登录Wiki网站,则需要使用“login”类型的令牌才能继续。

如需迁移旧代码的帮助,请参阅旧版API令牌参数的弃用

API帮助文档

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]


示例

GET请求

回应

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

示例代码

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 );
$output = curl_exec( $ch );
curl_close( $ch );

$result = json_decode( $output, true );
echo( $result['query']['tokens']['logintoken'] . "\n" );

Perl

#!/usr/bin/perl

# token.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";

JavaScript

/*
    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 );
} );

可能的错误

代码 信息

参数历史

  • v1.27: 启用login, createaccount

参阅 =