Getting Oauth Token

Hey guys,

I dont play often with APIs but a while ago I wrote a bit code too see if one of my friends is live and I used php and Curl to achieve this.

I used:

    $link = https://api.twitch.tv/helix/streams?user_login=XXX&user_login=YYY;

    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
         'Accept: application/vnd.twitchtv.v5+json',
         'Client-ID: *My-Client-ID*',
    ));
    $l1 = curl_exec($ch);
    curl_close($ch);

    $twitch_array = json_decode($l1, true);
    echo "Twitch Array 1: <br>";
    print_r($twitch_array);

All worked fine for me but now i get the error:
Array ( [error] => Unauthorized [status] => 401 [message] => OAuth token is missing )

I searched for this error and after a while I found out that Twitch changed there API so that every request needs an Oauth Token but I am not able to generate one of these for me.

I have a Client- and a Secret Id and this Link:

https://id.twitch.tv/oauth2/token
    --data-urlencode
    ?grant_type=refresh_token
    &refresh_token=REMOVED
    &client_id=fooid
    &client_secret=barbazsecret

Can someone explain me how I get the nessesary Oauth token with these 3 things using Curl?

I have this Code now:

    $url = 'https://id.twitch.tv/oauth2/token--data-urlencode?grant_type=refresh_token&refresh_token=REMMOVED&client_id=dM<-Client-ID&client_secret=My-Secret-ID';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    print_r($response);

I deleted the scope part because I think its not needed for “…/streams/…” and I dont know what I should use for the refresh token.

The error is:
{"status":400,"message":"invalid URL escape \"%6V\""}

Removed the refresh token

ClientID’s are public
ClientSecret, oAuth Tokens, and Refresh tokens are not and should be securet

This is very very wrong

--data-urlencode tells you what/how to format the data when using cURL it doesn’t become part of the URL.

$ch = curl_init('https://id.twitch.tv/oauth2/?grant_type=refresh_token&refresh_token=REMOVED&client_id=CLIENT&client_secret=SECRET');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);

But this only works if you have a refresh token to use, which I don’t think you do.

You should generate and use an App Access Token/Client Credentials for this

I understand now that the Link I used is for a refresh Token but the only thing I want is an simple Oauth Token that my Request:
$link = https://api.twitch.tv/helix/streams?user_login=XXX&user_login=YYY;

works like before the Twitch API Changes.

Then you need to generate and store then use an app access token. As covered in the oAuth docs I already linked.

Then pass that token with your request.

An example is on this other Post that is doing similar to what you are doing

But it’s not the recommended solution as this generates an oAuth token every time rather than reusing an old still valid one.

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