Twitch API - 要求auth令牌返回任何内容

时间:2014-07-24 12:15:51

标签: php json api curl twitch

我对此问题有类似的问题:Twitch API - can't get auth token using PHP但不能写评论。

尝试了代码:

    $ch = curl_init("https://api.twitch.tv/kraken/oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
    'client_id' => 'blablabla_correct',
    'client_secret' => 'blablabla_also_correct',
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://localhost/php/twitch.php',
    'code' => $_GET['code']
);


 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $data = curl_exec($ch);
print $data;

但它什么都没打印。所以我尝试使用#2中的代码调试请求:

// to start, just use the code you've already got:
$ch = curl_init("https://api.twitch.tv/kraken/oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
    'client_id' => 'blablabla_correct',
    'client_secret' => 'blablabla_also_correct',
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://localhost/php/twitch.php',
    'code' => $_GET['code']
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$data = curl_exec($ch);

// Now, here we believe the first error comes into play, so let's check it out
print_r($data); // confirm that this is not what we want
$info = curl_getinfo($ch); // let's get some details about that last request
// print it out and see what we get
echo '<pre>';
print_r($info);
echo '</pre>';

我得到了关注数组。我不知道接下来该做什么。 API文档位于:https://github.com/justintv/Twitch-API/blob/master/authentication.md#auth-code

Array
(
    [url] => https://api.twitch.tv/kraken/oauth2/token
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.203
    [namelookup_time] => 0
    [connect_time] => 0.203
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [primary_ip] => 192.16.71.172
    [primary_port] => 443
    [local_ip] => 192.168.1.100
    [local_port] => 51603
    [redirect_url] => 
)

1 个答案:

答案 0 :(得分:0)

我可能错了,但作为其他登录提供商,我可以想象,twitch也不允许redirect urlslocalhost这样的非现有域。

如果您想要localhost返回网址,我可以更喜欢这项服务,您可以将重定向网址指向通配符服务器,并将请求重定向到您的本地主机。

http://xip.io

  

xip.io是一个神奇的域名,可为任何IP提供通配符DNS   地址。假设您的LAN IP地址是10.0.0.1。使用xip.io,

     10.0.0.1.xip.io            resolves to   10.0.0.1
     www.10.0.0.1.xip.io        resolves to   10.0.0.1    
     mysite.10.0.0.1.xip.io     resolves to   10.0.0.1  
     foo.bar.10.0.0.1.xip.io    resolves to   10.0.0.1