0

我有类似的问题:Twitch API - can't get auth token using PHP but cannot write a comment。

试过代码:

    $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] => 
)
4

1 回答 1

0

我可能是错的,但作为其他登录提供商,我可以想象 twitch 也不允许redirect urls不存在的域,如localhost.

如果您想要 localhost 返回 url,我可以更喜欢此服务,您可以将重定向 url 指向通配符服务器,并且请求将重定向到您的 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
于 2014-07-24T12:32:33.620 回答