# Php get email and username

**URL:** <https://discuss.dev.twitch.com/t/php-get-email-and-username/25910>\
**Category:** API\
**Created:** [May 25, 2020, 5:07pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910 "2020-05-25T17:07:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrazyD00](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/crazyd00/32/8827_2.png) [@CrazyD00](https://discuss.dev.twitch.com/u/CrazyD00)\
**Post date:** [May 25, 2020, 5:07pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/1 "2020-05-25T17:07:36Z")

</div>

Hello,

I need to recover email and username.

For the moment with url: “[https://id.twitch.tv/oauth2/token](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](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.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 25, 2020, 5:34pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/2 "2020-05-25T17:34:17Z")

</div>

```
 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

---

<div class="post-metadata">

**Author:** ![CrazyD00](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/crazyd00/32/8827_2.png) [@CrazyD00](https://discuss.dev.twitch.com/u/CrazyD00)\
**Post date:** [May 25, 2020, 5:49pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/3 "2020-05-25T17:49:36Z")

</div>

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”}

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 25, 2020, 5:54pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/4 "2020-05-25T17:54:21Z")

</div>

And you checked that `$validate_token` contains the token?

I don’t see any other problem with your code

---

<div class="post-metadata">

**Author:** ![CrazyD00](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/crazyd00/32/8827_2.png) [@CrazyD00](https://discuss.dev.twitch.com/u/CrazyD00)\
**Post date:** [May 25, 2020, 5:58pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/5 "2020-05-25T17:58:36Z")

</div>

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'];
> 
> ```

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 25, 2020, 6:02pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/6 "2020-05-25T18:02:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CrazyD00](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/crazyd00/32/8827_2.png) [@CrazyD00](https://discuss.dev.twitch.com/u/CrazyD00)\
**Post date:** [May 25, 2020, 6:11pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/7 "2020-05-25T18:11:45Z")

</div>

> ```
> <?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>';
> }
> 
> ?>
> 
> ```

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 25, 2020, 6:14pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/8 "2020-05-25T18:14:48Z")

</div>

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](https://www.php.net/curl_setopt)

> **`TRUE`** to return the transfer as a string of the return value of [curl\_exec()](https://www.php.net/manual/en/function.curl-exec.php) instead of outputting it directly.

I just didn’t clock it in your original post

---

<div class="post-metadata">

**Author:** ![CrazyD00](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/crazyd00/32/8827_2.png) [@CrazyD00](https://discuss.dev.twitch.com/u/CrazyD00)\
**Post date:** [May 25, 2020, 6:19pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/9 "2020-05-25T18:19:48Z")

</div>

Nice !  
Its good thanks you im very stupid 😅

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [June 24, 2020, 6:19pm UTC](https://discuss.dev.twitch.com/t/php-get-email-and-username/25910/10 "2020-06-24T18:19:49Z")

</div>

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