抽动API - 使用PHP无法获得身份验证令牌 [英] Twitch API - can't get auth token using PHP

查看:509
本文介绍了抽动API - 使用PHP无法获得身份验证令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好计算器成员。
我不是谁喜欢问的帮助,但在这种情况下,它IMO解决我的问题的唯一途径的人。谷歌没有帮助我很多。

Hello stackoverflow members. I'm not a person who likes to ask for a help but in this case it's IMO the only way to solve my problem. Google didn't help me much.

所以。我的问题:
我想用抽搐的API来获取一些数据。听起来很简单?我希望它是。下面我张贴我的实际code(这是小,但它被修改各种时代和现在的样子...):

So. My problem: I want to get some data using Twitch API. Sounds easy? I wish it was. Below I'm posting my actual code (it's small but it was modified various of times and now it look like...):

$user = json_decode(file_get_contents('https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=MY_CORRECT_CLIENT_ID&redirect_uri=http://localhost/php/twitch.php&scope=user_read'), true);
print_r($user); // returns nothing

$token = $user['access_token'];
print_r($token); // same as above

$ch = curl_init();

// some stupid curls
curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/kraken/streams/followed'); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: OAuth '.$token ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$retval = curl_exec($ch);

curl_close($ch);

$result = json_decode($retval, true);

它返回什么都没有。所以我用从discussions.twitch现成的解决方案。 (我希望我能写这篇code的作者的名字,但我太累了再次搜索它的感谢无论哪种方式!)

It returns... Nothing. So I used ready solution from discussions.twitch. (I wish I could write the name of the author of this code but I'm too tired to search it again. Either way thanks!):

$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);
$response = json_decode($data, true);
//var_dump($response);
$access_token = $response["access_token"];
echo $access_token;

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $returnobj = curl_exec($ch);
    curl_close($ch);
    return $returnobj;
}

$testobj = json_decode(get_data("https://api.twitch.tv/kraken/user?oauth_token=".$access_token."&client_id=".$fields['client_id']));
echo "<br>Data: ";

print_r($testobj);

这$ C $上面c是一个好一点。只有一点点。它返回的错误401 。为什么?因为它无法获得的身份验证令牌的。那么,它的东西,但并不是我想要得到的。我现在应该怎么做?也许这是本地主机地址的错吗?

This code above is a bit better. Only a bit. It returns Error 401. Why? Because it can't get auth token. Well, it's something but not what I wanted to get. What should I do now? Maybe it's fault of localhost address?

FAQ(?):
是的,我使用的是从我的抽搐应用程序设置页面正确的数据。
是的,我很困惑

FAQ(?): Yes, I'm using correct data from my Twitch application settings page. Yes, I'm confused

推荐答案

您正在做两次调用API抽搐,并且你需要独立调试。

You're making two calls to the Twitch API, and you need to debug them independently.

现在,只需跳过第二个电话。重点放在你抓你的访问令牌的人。

For now, just skip the second call. Focus on the one where you grab your access token.

试试这个:

// 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>';

...这应该给你一个起点,要弄清楚是怎么回事。如果你看到一个身份验证令牌,那么你就不能以正确的方式访问它。如果你不这样做,信息会给你为什么一些信息。

... that should give you a starting point to figure out what's going on. If you see an auth token, then you're not accessing it in the right way. If you don't, the info will give you some information about why.

我不知道是什么 REDIRECT_URI 是(你可以链接到解释文档?),所以我不知道,如果本地主机的参考是个问题在那里。

I don't know what redirect_uri is (can you link to docs that explain it?) so I can't know if the localhost reference is a problem there.

这篇关于抽动API - 使用PHP无法获得身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆