API:监视
![]() | 本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.14 |
从监视列表添加或移除页面需要POST请求。
API帮助文档
示例
发出任何POST请求都是一个多步骤的过程:
- 使用API:登录 中描述的方法之一登录。
- 获取CSRF令牌 。
上述示例查询适用于运行MediaWiki 1.24+的Wiki。
对于运行MediaWiki早期版本的Wiki,可以通过
action=tokens
和type=watch
(MediaWiki 1.20+)或使用以下查询来获取此操作的令牌: - 发送带有CSRF令牌的POST请求以在页面上执行操作。
下面的示例代码详细介绍了最后一步。
POST请求
将页面添加到您的监视列表。
回应
{
"batchcomplete": "",
"watch": [
{
"title": "Stone Forest",
"watched": ""
}
]
}
示例代码
Python
#!/usr/bin/python3
"""
watch.py
MediaWiki API Demos
Demo of `Watch` module: Add a page to your watchlist
MIT license
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
# Step 1: Retrieve a login token
PARAMS_1 = {
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_1)
DATA = R.json()
LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"]
# Step 2: Send a post request to log in. For this login
# method, Obtain credentials by first visiting
# https://www.en.wikipedia.org/wiki/Special:BotPasswords
# See https://www.mediawiki.org/wiki/API:Login for more
# information on log in methods.
PARAMS_2 = {
"action": "login",
"lgname": "username",
"lgpassword": "password",
"format": "json",
"lgtoken": LOGIN_TOKEN
}
R = S.post(URL, data=PARAMS_2)
# Step 3: While logged in, retrieve a CSRF token
PARAMS_3 = {
"action": "query",
"meta": "tokens",
"type": "watch",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_3)
DATA = R.json()
CSRF_TOKEN = DATA["query"]["tokens"]["watchtoken"]
# Step 4: Post request to add a page to your watchlist
PARAMS_4 = {
"action": "watch",
"titles": "Stone forest",
"format": "json",
"token": CSRF_TOKEN,
}
R = S.post(URL, data=PARAMS_4)
DATA = R.json()
print(DATA)
PHP
<?php
/*
watch.php
MediaWiki API Demos
Demo of `Watch` module: Add a page to your watchlist
MIT license
*/
$endPoint = "https://test.wikipedia.org/w/api.php";
$login_Token = getLoginToken(); // Step 1
loginRequest( $login_Token ); // Step 2
$watch_Token = getWatchToken(); // Step 3
editWatchlist( $watch_Token ); // Step 4
// Step 1: GET request to fetch login token
function getLoginToken() {
global $endPoint;
$params1 = [
"action" => "query",
"meta" => "tokens",
"type" => "login",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params1 );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
return $result["query"]["tokens"]["logintoken"];
}
// Step 2: POST request to log in. Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest( $logintoken ) {
global $endPoint;
$params2 = [
"action" => "login",
"lgname" => "bot_user_name",
"lgpassword" => "bot_password",
"lgtoken" => $logintoken,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params2 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
}
// Step 3: GET request to fetch watch token
function getWatchToken() {
global $endPoint;
$params3 = [
"action" => "query",
"meta" => "tokens",
"type" => "watch",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params3 );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
return $result["query"]["tokens"]["watchtoken"];
}
// Step 4: POST request to add a page to your watchlist
function editWatchlist( $watch_Token ) {
global $endPoint;
$params4 = [
"action" => "watch",
"titles" => "Sandbox",
"token" => $watch_Token,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params4 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
echo( $output );
}
JavaScript
/*
watch.js
MediaWiki API Demos
Demo of `Watch` module: Add a page to your watchlist
MIT license
*/
var request = require('request').defaults({jar: true}),
url = "https://test.wikipedia.org/w/api.php";
// Step 1: GET request to fetch login token
function getLoginToken() {
var params_0 = {
action: "query",
meta: "tokens",
type: "login",
format: "json"
};
request.get({ url: url, qs: params_0 }, function (error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
loginRequest(data.query.tokens.logintoken);
});
}
// Step 2: POST request to log in.
// Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest(login_token) {
var params_1 = {
action: "login",
lgname: "bot_username",
lgpassword: "bot_password",
lgtoken: login_token,
format: "json"
};
request.post({ url: url, form: params_1 }, function (error, res, body) {
if (error) {
return;
}
getCsrfToken();
});
}
// Step 3: GET request to fetch watch token
function getCsrfToken() {
var params_2 = {
action: "query",
meta: "tokens",
type: "watch",
format: "json"
};
request.get({ url: url, qs: params_2 }, function(error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
watch(data.query.tokens.watchtoken);
});
}
// Step 4: POST request to add a page to your watchlist
function watch(watch_token) {
var params_3 = {
action: "watch",
titles: "Sandbox",
token: watch_token,
format: "json"
};
request.post({ url: url, form: params_3 }, function (error, res, body) {
if (error) {
return;
}
console.log(body);
});
}
// Start From Step 1
getLoginToken();
MediaWiki JS
/*
watch.js
MediaWiki API Demos
Demo of `Watch` module: Add a page to your watchlist
MIT License
*/
var params = {
action: 'watch',
titles: 'Sandbox',
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'watch', params ).done( function ( data ) {
console.log( data );
} );
参数历史
- v1.25: 棄用
uselang
- v1.23:
- 啟用
continue
、title
、pageids
、revids
、generator
、redirects
、converttitles
- 弃用
title
- 啟用
- v1.21: 启用
uselang
补充资料
- 当通过机器人运行示例代码时,请访问Special:BotPasswords页面,以确保该机器人的“编辑您的监视列表”选项设置为
true
。 - 这一模块使用CSRF令牌,而非监视列表令牌。 CSRF令牌通常用于Action API 中的POST请求和Wiki修改操作,而监视列表令牌专门用于查看其他用户的监视列表。
- 与API:监视列表 不同,API:监视列表 允许您无需登录即可阅读帐户的私人监视列表,该模块要求您直接登录要更改的帐户。
參見
- API:监视列表 - 获取监视列表
- API:监视列表订阅源 - 获取用户监视列表的RSS或Atom提要