问题描述:

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

网友答案:

just adding my 2 cents.

I've been doing a similar implementation in .NET MVC - I was able to get the token as you did, but the list=1 feature didn't work for me either, getting the 400 bad request code (as you have found).

The solution for me: In the token.html response, there is a token in the div and also a GUID in the header.

To break it down:

  1. Call token.html with uTorrent credentials
  2. In the response content, parse the html to get the token
  3. in the response header, there is a value with key Set-Cookie, which looks like

    Set-Cookie: GUID=<guid value>

  4. I needed to use this value (GUID=<guid value>) in all requests being sent back, as well as the token and it worked!

I'm not sure what the implementation is in PHP to do this however :)

Also quick note, I've been trying to get values through jQuery's $.getJSON and $.Ajax method without any success, because the browser (chrome) I'm using has strict guidelines on cross domain requests, and it doesn't look like uTorrent is implementing JSONP.

Hope this helps!

网友答案:

The 400 Error message means you are communicating with a bad request. The MIME media type for JSON text is application/json .

网友答案:

use text/plain or application/json, not text/json.

application/json sometimes causes issues on Chrome, so you might want to stick with text/plain in this case.

网友答案:

Have you tried changing the order of the query parameters?

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

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


UPDATE I ran into a similar problem trying to create and XMPPBot for utorrent client in python.

@m.t.bennett was correct. You need to save the session information as well.

When you receive the response from token.html, capture the cookie information as well. Usually there are 2 params: GUID and sessions. You need to put them in the header for all your subsequent requests -- List API, Getfiles API, etc.

This should fix your problem!

相关阅读:
Top