简体   繁体   English

使用Mediawiki API扩展中的错误或无效令牌

[英]Bad or Invalid Token in Extension with Mediawiki API

I'm trying to make a simple extension that moves a page on my mediawiki site, but whether using curl or FauxRequest, I always get bad token response. 我正在尝试进行一个简单的扩展,在我的mediawiki网站上移动一个页面,但无论是使用curl还是FauxRequest,我总是得到错误的令牌响应。 Tried urlencoding, not encoding, escaping, without +/, etc, doesn't matter. 尝试urlencoding,而不是编码,转义,没有+ /等,无关紧要。

Code looks like this currently using FauxRequest, with param1/2/3 coming from the parser function I'm creating. 代码看起来像目前正在使用FauxRequest,param1 / 2/3来自我正在创建的解析器函数。

global $wgRequest;
$token = $token = $wgUser->editToken();

$params = new FauxRequest( 
    array(
        'action'    => 'move',
        'from'      => $param1,
        'to'        => $param2,
        'format'    => 'php',
        'reason'    => $param3,
        'token'     => $token)
);
$api = new ApiMain( $params, true);
$api->execute();
$data = & $api->getResultData();


$output = "moved $param1 to $param2 - $token";

also tried the below code using curl instead, which results in bad token as well 也尝试使用curl代替下面的代码,这也会导致错误的令牌

global $wgUser;
$token = $wgUser->editToken();
$url = 'http://www.website.com/api.php?';
    $myvars = 'action=move&format=xml&from=' . "$param1" . '&to=' . "$param2" . '&reason=' . "$param3" . '&token=' . urlencode($token);

    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec( $ch );


$output = "moved $param1 to $param2 - $myvars - $response";

Am I missing something in the code, or could I have a setting wrong somewhere? 我在代码中遗漏了什么,或者我可能在某处设置错误?

Any help would be hugely appreciated! 任何帮助将非常感谢!

The actual error might be something else. 实际的错误可能是别的。 Did you try doing it as specified in https://www.mediawiki.org/wiki/API:Calling_internally 您是否尝试按照https://www.mediawiki.org/wiki/API:Calling_internally中的说明进行操作

I was recommended to not use either method for moving a page in an extension, and was instructed to use the following code instead, which works great. 我被建议不要使用任何一种方法在扩展中移动页面,并且被指示使用以下代码,这非常有用。 Hope this helps someone. 希望这有助于某人。

$oldTitle = Title::newFromText( $param1 );
$newTitle = Title::newFromText( $param2 );
// Error checking here
$oldTitle->moveTo( $newTitle, true, $param3, true );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM