Twitch API - 无法使用PHP获取身份验证令牌

时间:2014-05-19 20:22:14

标签: php api curl json twitch

Hello stackoverflow成员。 我不是一个喜欢寻求帮助的人,但在这种情况下,IMO是解决问题的唯一方法。谷歌对我帮助不大。

因此。我的问题: 我想使用Twitch API获取一些数据。听起来很容易?我希望是的。下面我发布了我的实际代码(它很小但是它被修改了很多次,现在它看起来像......):

$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);

它返回......没什么。所以我使用了discussion.twitch的现成解决方案。 (我希望我能写出这段代码作者的名字,但我太累了,无法再次搜索。无论如何,谢谢!):

$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);

上面这段代码好一点。只有一点点。它返回错误401 。为什么?因为它无法获得身份验证令牌。嗯,这是一些东西,但不是我想要的东西。我现在应该怎么做?也许它是localhost地址的错?

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

1 个答案:

答案 0 :(得分:1)

您正在对Twitch API进行两次调用,您需要单独调试它们。

现在,只需跳过第二个电话。专注于您获取访问令牌的那个。

试试这个:

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

......这应该给你一个起点,弄清楚发生了什么。如果您看到身份验证令牌,那么您无法以正确的方式访问它。如果您不知道,该信息将为您提供有关原因的信息。

我不知道redirect_uri是什么(你可以链接到解释它的文档吗?)所以我无法知道localhost引用是否存在问题。