【问题标题】:When calling the MediaWiki-API I only get '+\' as an csrf-token调用 MediaWiki-API 时,我只得到 '+\' 作为 csrf-token
【发布时间】:2015-02-16 20:10:11
【问题描述】:

我可以使用简单的令牌“+\”进行编辑,但使用这个简单的令牌,我只能作为未注册的 IP 进行编辑,而不能作为注册用户进行编辑。有人可以帮我吗?

我的 PHP 代码:

$parameters = array('action' => 'query', 'meta' => 'tokens', 'format' => 'json');
$options = array(
   'http' => array(
      'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
      'method'  => 'POST',
      'content' => http_build_query($parameters),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($wiki, false, $context);
echo "$result";

【问题讨论】:

  • 我建议将 cURL 用于此类事情。使其更清洁并为您处理所有协议内容。

标签: php mediawiki


【解决方案1】:

您需要登录才能获得真正的 CSRF 令牌。登录方式详见:

http://www.mediawiki.org/wiki/API:Login

请注意,保持登录状态需要您跨请求存储 cookie。 file_get_contents() 将很难做到这一点;建议使用带有 cookie jar 的 cURL。

【讨论】:

  • 我试过不使用 cURL 但它不起作用:'http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n Set -Cookie: $cookieprefix = $sessionid;\r\n dewikiUserName = $lgname;\r\n dewikiToken = $token", 'method' => 'POST', 'content' => http_build_query($parameters), ),
  • @mischa004 那肯定行不通——客户端标头应该是Cookie,而不是Set-Cookie
【解决方案2】:

要获取login token,请使用type=login 参数。示例:

curl 'https://www.mediawiki.org/w/api.php?action=query&meta=tokens&format=json&type=login'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2015-04-15
    • 2023-03-26
    • 2021-10-19
    • 2021-03-26
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多