1

I am trying to access utorrents web api, it uses a token authentication system which is detailed here

the JavaScript on my page is

        <script>
            $.getJSON("http://XXX.XXX.XXX.XXX/lib/token.php", function(response) {
                var head = document.getElementsByTagName('head')[0];
                var script = document.createElement('script');
                script.type = 'text/javascript';
                //script.onreadystatechange = function () {
                //    if (this.readyState == 'complete') utorrent();
                //}
                //script.onload = utorrent();
                script.src = 'http://XXX.XXX.XXX.XXX:8080/gui/?list=1&token=' + response.token;
                head.appendChild(script);
            });
        </script>

simply retrieving the token from a php file and passing it along the chain, i have confirmed that the token is being passed and is not being poisonned, my PHP document is below

<?php
header('Content-type: text/json');
$token = file_get_contents('http://[username]:[password]@XXX.XXX.XXX.XXX:8080/gui/token.html');
$token = str_replace("<html><div id='token' style='display:none;'>", "", $token);
$token = str_replace("</div></html>", "", $token);
$response = array('token' => $token);
echo json_encode($response);
?>

this gives me a confirmation of the token Object {token: "GMt3ryaJE64YpXGN75-RhSJg-4gOW8n8XfTGYk_ajpjNLNLisR3NSc8tn1EAAAAA"}

but then i receive a 400 error code when retrieving the list GET http://XXX.XXX.XXX.XXX:8080/gui/?list=1&token=GMt3ryaJE64YpXGN75-RhSJg-4gOW8n8XfTGYk_ajpjNLNLisR3NSc8tn1EAAAAA 400 (ERROR)

Any help/thoughts/idea's would be greatly appreciated

4

4 回答 4

1

只需加上我的 2 美分。

我一直在 .NET MVC 中进行类似的实现——我能够像你一样获得令牌,但是 list=1 功能对我也不起作用,得到 400 bad request code(如你所见) .

我的解决方案:在 token.html 响应中,div 中有一个令牌,标题中还有一个 GUID

分解它:

  1. 使用 uTorrent 凭据调用 token.html
  2. 在响应内容中,解析html获取token
  3. 在响应标头中,有一个带有 key 的值Set-Cookie,看起来像

    Set-Cookie: GUID=<guid value>

  4. 我需要GUID=<guid value>在所有发回的请求中使用这个值(),以及令牌,它起作用了!

但是我不确定 PHP 中的实现是什么:)

另请注意,我一直在尝试通过 jQuery$.getJSON$.Ajax方法获取值但没有成功,因为我使用的浏览器(chrome)对跨域请求有严格的指导方针,而且它看起来不像 uTorrent 正在实施JSONP

希望这可以帮助!

于 2013-07-16T05:58:27.057 回答
0

使用text/plainor application/json, not text/json

application/jsontext/plain有时会导致 Chrome 出现问题,因此您可能希望在这种情况下坚持使用。

于 2013-05-24T09:45:01.917 回答
0

400 错误消息意味着您正在与错误的请求进行通信。JSON 文本的 MIME 媒体类型是 application/json 。

于 2013-05-24T09:52:13.177 回答
0

您是否尝试过更改查询参数的顺序?

例如:http://localhost:8080/gui/?token=<token_uuid>&list=1

参考:https ://github.com/bittorrent/webui/wiki/TokenSystem#examples


更新 我在尝试为 python 中的 utorrent 客户端创建和 XMPPBot 时遇到了类似的问题。

@mtbennett 是正确的。您还需要保存会话信息。

当您收到来自token.html的响应时,请同时捕获 cookie 信息。通常有 2 个参数:GUIDsession。您需要将它们放在所有后续请求的标头中——List API、Getfiles API 等。

这应该可以解决您的问题!

于 2014-01-26T05:49:35.297 回答