【问题标题】:Struggling to communicate with the utorrent web API努力与 utorrent Web API 通信
【发布时间】:2013-05-24 09:10:44
【问题描述】:

我正在尝试访问 utorrents web api,它使用令牌身份验证系统,详细说明 here

我页面上的 JavaScript 是

        <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>

只需从 php 文件中检索令牌并将其沿链传递,我已确认令牌正在传递并且没有被毒化,我的 PHP 文档在下面

<?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);
?>

这给了我对令牌的确认 对象{令牌:“GMt3ryaJE64YpXGN75-RhSJg-4gOW8n8XfTGYk_ajpjNLNLisR3NSc8tn1EAAAAA”}

但是我在检索列表时收到 400 错误代码 获取http://XXX.XXX.XXX.XXX:8080/gui/?list=1&token=GMt3ryaJE64YpXGN75-RhSJg-4gOW8n8XfTGYk_ajpjNLNLisR3NSc8tn1EAAAAA 400(错误)

任何帮助/想法/想法将不胜感激

【问题讨论】:

    标签: javascript php utorrent


    【解决方案1】:

    只需加上我的 2 美分。

    我一直在 .NET MVC 中进行类似的实现 - 我能够像您一样获取令牌,但 list=1 功能对我也不起作用,得到 400 错误请求代码(如您找到了)。

    我的解决方案: 在 token.html 响应中,div 中有一个标记,并且标头中有一个 GUID

    分解:

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

      Set-Cookie: GUID=&lt;guid value&gt;

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

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

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

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        使用text/plainapplication/json,而不是text/json

        application/json 有时会导致 Chrome 出现问题,因此在这种情况下您可能需要坚持使用text/plain

        【讨论】:

          【解决方案4】:

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

          例如:http://localhost:8080/gui/?token=&lt;token_uuid&gt;&amp;list=1

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


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

          @m.t.bennett 是正确的。您还需要保存会话信息。

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-27
            • 2023-04-03
            • 2013-04-28
            • 1970-01-01
            • 2015-01-20
            • 2015-12-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多