Php get email and username

Hello,

I need to recover email and username.

For the moment with url: “https://id.twitch.tv/oauth2/token
I have “access_token”, “expires_in”, “refresh_token”, “scope”: [“user_read”], “token_type”: “bearer”.

however following in my code I use url: “https://api.twitch.tv/helix/users
I got: {“error”: “Unauthorized”, “status”: 401, “message”: “OAuth token is missing”}

$validate_token is ‘access_token’

 $ch = curl_init ();

 $url = 'https://api.twitch.tv/helix/users';

 curl_setopt ($ch, CURLOPT_URL, $url);
 curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'GET');
 curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
     'Authorization: Bearer'. $validate_token,
     'Client-ID:'. $clientID
 ));
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, false);

 $response = curl_exec ($ ch);
 echo $response;

 curl_close ($ ch);

With reqbin i got a good response json but with my website i don’t got.

 curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
     'Authorization: Bearer'. $validate_token,
     'Client-ID:'. $clientID
 ));

should be

 curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
     'Authorization: Bearer '. $validate_token,
     'Client-ID:'. $clientID
 ));

You were missing the space after Bearer and before the token

Its not a solution,

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer '. $validate_token,
    'Client-ID: '. $clientID
));

I add a space in 'Authorization: Bearer ’ and 'Client-ID: ’

again : {“error”:“Unauthorized”,“status”:401,“message”:“OAuth token is missing”}

And you checked that $validate_token contains the token?

I don’t see any other problem with your code

for token :

$ch = curl_init();

$url = 'https://id.twitch.tv/oauth2/token';

$opts = [
CURLOPT_URL            => $url,
CURLOPT_CUSTOMREQUEST  => 'POST',
CURLOPT_RETURNTRANSFER => false

];

$post = [
    'client_id' => $clientID,
    'client_secret' => $sclientID,
    'code' => $token,
    'grant_type' => 'authorization_code',
    'redirect_uri'  => 'http://tworld.lescigales.org/api.php'
        ];

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);

$json_array = json_decode($response, true);
curl_close($ch);

$validate_token = $json_array['access_token'];

This doesn’t tell me if $validate_token is available to your first bit of code.

You may have wrapped the first bit in a function and not made the token available inside that function.

So might be easier to show all your code rather than parts of your code

<?php
	$clientID = 'clientid';
	$sclientID = 'clientsecret';

if(!empty($_GET["code"])) {

	$token = $_GET["code"];
	$tokenid= $_GET["id_token"];

    echo $tokenid;

        //  PRMEIERE REQUETE

    $ch = curl_init();

    $url = 'https://id.twitch.tv/oauth2/token';

    $opts = [
    CURLOPT_URL            => $url,
    CURLOPT_CUSTOMREQUEST  => 'POST',
    CURLOPT_RETURNTRANSFER => false

    ];

    $post = [
        'client_id' => $clientID,
        'client_secret' => $sclientID,
        'code' => $token,
        'grant_type' => 'authorization_code',
        'redirect_uri'  => 'http://tworld.lescigales.org/api.php'
            ];

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt_array($ch, $opts);
    $response = curl_exec($ch);

    $json_array = json_decode($response, true);
    curl_close($ch);

    $validate_token = $json_array['access_token'];
    //$userinfo = $json_array['userinfo'];
    //echo $userinfo;

    echo '<br><br><br>';

        //  SECONDE REQUETE

    $ch = curl_init();

    $url = 'https://api.twitch.tv/helix/users';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
     curl_setopt ($ch, CURLOPT_HTTPHEADER, array (
         'Authorization: Bearer '. $validate_token,
         'Client-ID: '. $clientID
     ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

    $response = curl_exec($ch);
    echo $response;

    curl_close($ch);

    $json_array = json_decode($response, true);
    echo $json_array;
}

else{
	echo '<a href="https://id.twitch.tv/oauth2/authorize?client_id=' . $clientID . '&redirect_uri=http://tworld.lescigales.org/api.php&response_type=code&scope=user_read">Connexion</a>';
}

?>

Theres your problem

CURLOPT_RETURNTRANSFER => false

and/or

curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

This should be true for both requests.

Other wise

$response = curl_exec($ch);

Contains nothing useful

https://www.php.net/curl_setopt

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

I just didn’t clock it in your original post

Nice !
Its good thanks you im very stupid :sweat_smile:

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