API Request does not work anymore!

Wehre this is: 'Authorization: Bearer ’ . $token

if(isset($_GET['post'])) {
define("CLIENT_ID", "xxx");
define("CLIENT_SECRET", "xxx");

$keys = false;
if (file_exists(__DIR__ . '/auth.json')) {
    $keys = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}

$generate_token = true;
if ($keys) {
    // validate the token

    $ch99 = curl_init('https://id.twitch.tv/oauth2/validate');
    curl_setopt($ch99, CURLOPT_HTTPHEADER, array(
        'Authorization: OAuth ' . $keys->access_token
    ));
    curl_setopt($ch99, CURLOPT_RETURNTRANSFER, true);

    $r = curl_exec($ch99);
    $i = curl_getinfo($ch99);
    curl_close($ch);

    if ($i['http_code'] == 200) {
        // the token appears good
        $generate_token = false;

        // optional to check the expires
        $token = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            if ($token->expires_in < 3600) {
                // less than an hour left
                // make a new token
                echo 'Token close to expire. Regenerate';
                $generate_token = true;
            }
        } else {
            echo 'Failed to parse JSON. Assume dead token';
            $generate_token = true;
        }
    }
}

if ($generate_token) {
    $ch99 = curl_init('https://id.twitch.tv/oauth2/token');
    curl_setopt($ch99, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch99, CURLOPT_POST, true);
    curl_setopt($ch99, CURLOPT_POSTFIELDS, array(
        'client_id' => CLIENT_ID,
        'client_secret' => CLIENT_SECRET,
        'grant_type' => "client_credentials"
    ));

    $r = curl_exec($ch99);
    $i = curl_getinfo($ch99);
    curl_close($ch99);

    if ($i['http_code'] == 200) {
        $token = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            echo 'Got token';
            print_r($token);

            // store the token for next run
            file_put_contents(__DIR__ . '/auth.json', $r, JSON_PRETTY_PRINT);
        } else {
            echo 'Failed to parse JSON';
        }
    } else {
        echo 'Failed with ' . $i['http_code'] . ' ' . $r;
    }
} else {
    echo 'Token OK'; echo "</br>"; echo "</br>";
    print_r($keys);
}
 $gameName = htmlspecialchars($_POST['name1']);
 $channelsApi3 = 'https://api.twitch.tv/helix/games?name=';
 $clientId = 'xxx';
 $ch3 = curl_init();
 $apiVers3 = '&api_version=5';
 $uri3 = '&redirect_uri=https://smartsblog.optimal-options.de/';
 
 curl_setopt_array($ch3, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId,
'Authorization: Bearer ' . $token
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi3 . urlencode($gameName) . $apiVers3 . $uri3
 ));

 $response3 = curl_exec($ch3);

This is just the generator function, not your API calling function need the whole thing really…

You are putting the keys into $token when you should be using $keys.
Since if the $keys (loaded from the JSON file) are valid then it won’t make a $token.

So $token = json_decode($r); should be $keys = json_decode($r);

And the oAuth header in your code/call to the API should be

curl_setopt_array($ch3, array(
CURLOPT_HTTPHEADER => array(
'Client-ID: ’ . $clientId,
'Authorization: Bearer ' . $keys['access_token']
),

Uncaught Error: Cannot use object of type stdClass as array

My mistake it’s $keys->access_token

This was why I asked for all your code as I could of run it on my machine and tested it with my keys

so there must be: Bearer instead of OAuth?

Or here 'Authorization: Bearer ’ . $keys[‘access_token’] even OAuth??

Helix uses the prefix “Bearer”

So what to change?

Show current code please

if(isset($_GET['post'])) {
define("CLIENT_ID", "xxx");
define("CLIENT_SECRET", "xxx");

$keys = false;
if (file_exists(__DIR__ . '/auth.json')) {
    $keys = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}

$generate_token = true;
if ($keys) {
    // validate the token

    $ch99 = curl_init('https://id.twitch.tv/oauth2/validate');
    curl_setopt($ch99, CURLOPT_HTTPHEADER, array(
        'Authorization: OAuth ' . $keys->access_token //YOU SAID SOMETHING WRONG WITH THIS!!
    ));
    curl_setopt($ch99, CURLOPT_RETURNTRANSFER, true);

    $r = curl_exec($ch99);
    $i = curl_getinfo($ch99);
    curl_close($ch);

    if ($i['http_code'] == 200) {
        // the token appears good
        $generate_token = false;

        // optional to check the expires
        $token = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            if ($token->expires_in < 3600) {
                // less than an hour left
                // make a new token
                echo 'Token close to expire. Regenerate';
                $generate_token = true;
            }
        } else {
            echo 'Failed to parse JSON. Assume dead token';
            $generate_token = true;
        }
    }
}

if ($generate_token) {
    $ch99 = curl_init('https://id.twitch.tv/oauth2/token');
    curl_setopt($ch99, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch99, CURLOPT_POST, true);
    curl_setopt($ch99, CURLOPT_POSTFIELDS, array(
        'client_id' => CLIENT_ID,
        'client_secret' => CLIENT_SECRET,
        'grant_type' => "client_credentials"
    ));

    $r = curl_exec($ch99);
    $i = curl_getinfo($ch99);
    curl_close($ch99);

    if ($i['http_code'] == 200) {
        $token = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            echo 'Got token';
            print_r($token);

            // store the token for next run
            file_put_contents(__DIR__ . '/auth.json', $r, JSON_PRETTY_PRINT);
        } else {
            echo 'Failed to parse JSON';
        }
    } else {
        echo 'Failed with ' . $i['http_code'] . ' ' . $r;
    }
} else {
    echo 'Token OK'; echo "</br>"; echo "</br>";
    print_r($keys);
}

 $gameName = htmlspecialchars($_POST['name1']);
 $channelsApi3 = 'https://api.twitch.tv/helix/games?name=';
 $clientId = 'xxx';
 $ch3 = curl_init();
 $apiVers3 = '&api_version=5';
 $uri3 = '&redirect_uri=https://smartsblog.optimal-options.de/';
 
 curl_setopt_array($ch3, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId,
'Authorization: Bearer ' . $keys['access_token']
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi3 . urlencode($gameName) . $apiVers3 . $uri3
 ));

 $response3 = curl_exec($ch3);

This line

'Authorization: Bearer ’ . $keys[‘access_token’]

should be

'Authorization: Bearer ’ . $keys->access_token

You do not need this two lines

 $apiVers3 = '&api_version=5';
 $uri3 = '&redirect_uri=https://smartsblog.optimal-options.de/';

Heres the full corrected code that should work


if(isset($_GET['post'])) {
define("CLIENT_ID", "xxx");
define("CLIENT_SECRET", "xxx");

$keys = false;
if (file_exists(__DIR__ . '/auth.json')) {
    $keys = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}

$generate_token = true;
if ($keys) {
    // validate the token

    $ch99 = curl_init('https://id.twitch.tv/oauth2/validate');
    curl_setopt($ch99, CURLOPT_HTTPHEADER, array(
        'Authorization: OAuth ' . $keys->access_token
    ));
    curl_setopt($ch99, CURLOPT_RETURNTRANSFER, true);

    $r = curl_exec($ch99);
    $i = curl_getinfo($ch99);
    curl_close($ch);

    if ($i['http_code'] == 200) {
        // the token appears good
        $generate_token = false;

        // optional to check the expires
        $token = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            if ($token->expires_in < 3600) {
                // less than an hour left
                // make a new token
                echo 'Token close to expire. Regenerate';
                $generate_token = true;
            }
        } else {
            echo 'Failed to parse JSON. Assume dead token';
            $generate_token = true;
        }
    }
}

if ($generate_token) {
    $ch99 = curl_init('https://id.twitch.tv/oauth2/token');
    curl_setopt($ch99, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch99, CURLOPT_POST, true);
    curl_setopt($ch99, CURLOPT_POSTFIELDS, array(
        'client_id' => CLIENT_ID,
        'client_secret' => CLIENT_SECRET,
        'grant_type' => "client_credentials"
    ));

    $r = curl_exec($ch99);
    $i = curl_getinfo($ch99);
    curl_close($ch99);

    if ($i['http_code'] == 200) {
        $keys = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            echo 'Got token';
            // store the token for next run
            file_put_contents(__DIR__ . '/auth.json', $r, JSON_PRETTY_PRINT);
        } else {
            echo 'Failed to parse JSON';
        }
    } else {
        echo 'Failed with ' . $i['http_code'] . ' ' . $r;
    }
} else {
    echo 'Token OK'; echo "</br>"; echo "</br>";
    print_r($keys);
}

 $gameName = htmlspecialchars($_POST['name1']);
 $channelsApi3 = 'https://api.twitch.tv/helix/games?name=';
 $clientId = 'xxx';
 $ch3 = curl_init();
 
 curl_setopt_array($ch3, array(
    CURLOPT_HTTPHEADER => array(
        'Client-ID: ' . $clientId,
        'Authorization: Bearer ' . $keys->access_token
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi3 . urlencode($gameName)
 ));

 $response3 = curl_exec($ch3);

I have only “fixed” your code so it should work and not tidied it

Great Job , works fine, thank!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.