【问题标题】:Twitch API - can't get auth token using PHPTwitch API - 无法使用 PHP 获取身份验证令牌
【发布时间】:2014-07-07 22:23:23
【问题描述】:

你好 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);

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

$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。为什么?因为它无法获得auth token。嗯,这是一些东西,但不是我想要的。我现在该怎么办?也许是本地主机地址的错?

常见问题解答(?): 是的,我使用的是 Twitch 应用程序设置页面中的正确数据。 是的,我很困惑

【问题讨论】:

    标签: php api curl json twitch


    【解决方案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 是什么(你能链接到解释它的文档吗?)所以我不知道本地主机引用是否存在问题。

    【讨论】:

    • 如果您可以在第一个调试步骤之后提供更多信息,那么我们可以尝试再进行一步。
    • redirect_uri 来自 API 文档,它应该在这个请求中。您的代码工作正常,不知何故它是我的问题的解决方案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2021-04-21
    • 2023-03-08
    • 2020-08-05
    相关资源
    最近更新 更多