What am I doing wrong? Just trying to grab User ID in PHP

I’m running a twitch guide website, and instead of asking people to join with username / pass. I wanted to just allow them to sign in with their twitch account quickly. So all I want to grab is a users User Name and User ID for me to store so when they post on my site it will be stored against a User-ID / Username.

I assume my “Login” button should just be a link to:

https://api.twitch.tv/kraken/oauth2/authorize?client_id=<REMOVED>&redirect_uri=http://localhost/authentication&response_type=code&scope=channel_read

This works and redirects to to my page where I grab the “code” from the GET and pass it to my first function (Called Step 1)

Step1 Function

function step1($code) {

$client_id = "<REMOVED>";
$redirect_uri = "http://localhost/authentication";
$client_secret = "<REMOVED>";
$twitch_url = "https://api.twitch.tv/kraken/oauth2/token?client_id=".$client_id."&client_secret=".$client_secret."&code=".$code."&grant_type=authorization_code&redirect_uri=".$redirect_uri."&state=tom";

$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ($curl, CURLOPT_URL, $twitch_url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result = curl_exec($curl);
curl_close($curl);
$json_array = json_decode($result, true);
return array($json_array);

}

This works fine and returns:

Array
(
[0] => Array
(
[access_token] => “REMOVED”
[refresh_token] => “REMOVED”
[scope] => Array
(
[0] => channel_read
)

    )

)

I then take the access_token and pass it to my step2 function

Step2 Function

function step2($access_token) {

$client_id = "<REMOVED>";
$curl = curl_init();

$headers = array(
	'Accept: application/vnd.twitchtv.v5+json',
	'Client-ID:' . $client_id,
	'Authorization: OAuth ' . $access_token,
);
	
$twitch_url = "https://api.twitch.tv/kraken/user";

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($curl, CURLOPT_URL, $twitch_url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result = curl_exec($curl);
curl_close($curl);
$json_array = json_decode($result, true);
return array($json_array);

}

This gives me a response of:

Array
(
[0] => Array
(
[error] => Unauthorized
[status] => 401
[message] => error getting authorization token
)

)

Am I missing a step? Or doing something wrong, also I apologise if the CURL is a bit copy and paste code, its my first time using CURL

You’ve posted a valid oauth token in a public forum

I thought I had removed them all, I’ve just removed the last 2

Make sure to reset them too, as they’ll still be in people’s email notifications and possible web caches.

/kraken/user requires user_read, you’re using channel_read scope on the authorization process, which would be for /kraken/channel.

2 Likes

BINGO!

Its now working.

Also I’ve reset the Client Secret. Cheers!!

1 Like

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