【问题标题】:Twitch API - asked for auth token return nothingTwitch API - 要求授权令牌不返回任何内容
【发布时间】:2014-07-24 12:15:50
【问题描述】:

我有类似的问题: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] => 
)

【问题讨论】:

    标签: php json api curl twitch


    【解决方案1】:

    我可能是错的,但作为其他登录提供商,我可以想象 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
    

    【讨论】:

    • 仍然没有,但感谢您展示 xip.io 网站,它在未来会很有用 :) 顺便说一句。来自Twitch App“这必须与传递给授权端点的redirect_uri参数完全匹配。在本地测试时,您可以将其设置为localhost。”
    • 在现场试用过 - 没有。
    猜你喜欢
    • 2016-04-07
    • 2018-06-11
    • 2017-10-16
    • 2019-07-20
    • 2021-01-27
    • 1970-01-01
    • 2016-09-06
    • 2014-03-26
    • 2018-07-27
    相关资源
    最近更新 更多