# New api break my old code

**URL:** <https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359>\
**Category:** API\
**Created:** [May 4, 2020, 6:53pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359 "2020-05-04T18:53:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuele\_Capizzi](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/emanuele_capizzi/32/6915_2.png) [@Emanuele\_Capizzi](https://discuss.dev.twitch.com/u/Emanuele_Capizzi)\
**Post date:** [May 4, 2020, 6:53pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/1 "2020-05-04T18:53:46Z")

</div>

Hi, today my code stop working.  
I think that require new oauth method, how i can change this?

```
   $all_channels = [];
    foreach($channels as $chans){
    if (count($chans) > 0) {
    $callAPI = implode('&user_login=', $chans);
    $url="https://api.twitch.tv/helix/streams?user_login=" . $callAPI;
    $ch = curl_init();
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm'
  
));

$result = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);

// preparo le stamp per le API
$str = json_decode($result, true);
if ($i['http_code'] == 200) {
    // play print API
    foreach ($str['data'] as $live_chan){
        $all_channels[] = $live_chan;
    }
    // do stuff with $str has chnanels
} else {
    // non 200 do somethign with the error
    // do stuff with $str has error message
}
// fine chiamata API
}
}
```

---

<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 4, 2020, 6:54pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/2 "2020-05-04T18:54:44Z")

</div>

> [@New API rules breaking old script, help](https://discuss.dev.twitch.com/t/new-api-rules-breaking-old-script-help/25307/9):
>
> This is generating a LOT of tokens. You should generate a token, and use it till it’s close to expirartion, and then generate a new one, if you can. Thats standard, it’ll take 3-5 minutes (or quicker) for a stream to show as live in the API You may also be interested in [https://dev.twitch.tv/docs/api/webhooks-guide](https://dev.twitch.tv/docs/api/webhooks-guide) So twitch can tell you when a stream is live rather than long polling

has a PHP example that’ll help you

---

<div class="post-metadata">

**Author:** ![Emanuele\_Capizzi](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/emanuele_capizzi/32/6915_2.png) [@Emanuele\_Capizzi](https://discuss.dev.twitch.com/u/Emanuele_Capizzi)\
**Post date:** [May 4, 2020, 7:10pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/3 "2020-05-04T19:10:46Z")

</div>

should i add  
` 'Authorization: OAuth <access token>'`  
on curl httpheader array?

if that’s okay how i generate OAuth 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 4, 2020, 7:12pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/4 "2020-05-04T19:12:27Z")

</div>

> [@Emanuele\_Capizzi](#):
>
> if that’s okay how i generate OAuth token?

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

> [@Emanuele\_Capizzi](#):
>
> ‘Authorization: OAuth ’

No it’s Bearer for Helix not “OAuth”

---

<div class="post-metadata">

**Author:** ![Emanuele\_Capizzi](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/emanuele_capizzi/32/6915_2.png) [@Emanuele\_Capizzi](https://discuss.dev.twitch.com/u/Emanuele_Capizzi)\
**Post date:** [May 4, 2020, 7:29pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/5 "2020-05-04T19:29:44Z")

</div>

I’ve make this but don’t work.

```
$cho = curl_init();
    curl_setopt($cho, CURLOPT_URL, 'https://api.twitch.tv/kraken/oauth2/token');
    curl_setopt($cho, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($cho, CURLOPT_POST, 1);
    curl_setopt($cho, CURLOPT_POSTFIELDS, http_build_query([
        'client_id' => 'c533u8ntdp29h42xxzwfe0pf5v66dm',
        'client_secret' => 'REMOVED',
        'grant_type' => 'client_credentials',
        'code' => $_GET['code']
    ]));
    $output = curl_exec($cho);
    curl_close($cho);
    $oauth = json_decode($output, true);
    // start call API
    $all_channels = [];
    foreach($channels as $chans){
        if (count($chans) > 0) {
        $callAPI = implode('&user_login=', $chans);
        $url="https://api.twitch.tv/helix/streams?user_login=" . $callAPI;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer ' $oauth ''
            'Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm'
        ));
        $result = curl_exec($ch);
        $i = curl_getinfo($ch);
        curl_close($ch);
```

---

<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 4, 2020, 7:31pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/6 "2020-05-04T19:31:47Z")

</div>

> [@Emanuele\_Capizzi](#):
>
> curl\_setopt($cho, CURLOPT\_URL, ‘[https://api.twitch.tv/kraken/oauth2/token](https://api.twitch.tv/kraken/oauth2/token)’);

Wrong URL

> [@Emanuele\_Capizzi](#):
>
> ‘client\_secret’ =\> ’

**DO NOT LEAK YOUR CLIENT SECRET IT IS A PASSWORD**

Please refer to the docs.

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

---

<div class="post-metadata">

**Author:** ![Emanuele\_Capizzi](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/emanuele_capizzi/32/6915_2.png) [@Emanuele\_Capizzi](https://discuss.dev.twitch.com/u/Emanuele_Capizzi)\
**Post date:** [May 4, 2020, 7:58pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/7 "2020-05-04T19:58:14Z")

</div>

Now i changed my code, but not work, i’m going crazy ç\_ç

```
$cho = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($cho, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cho, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cho, CURLOPT_POST, 1);
$fields = array(
    'client_id' => 'c533u8ntdp29h42xxzwfe0pf5v66dm',
    'client_secret' => 'xxxxxxxxxxx',
    'grant_type' => 'client_credentials',
    'token_type' => 'bearer',
    'state' => $_GET['state']
);
curl_setopt($cho, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec($cho);
$info = curl_getinfo($cho); 

print_r($info);

$oauth = json_decode($output, true);
$token = $oauth['access_token'];

print_r($token);

// start call API

$all_channels = [];
foreach($channels as $chans){
    if (count($chans) > 0) {
    $callAPI = implode('&user_login=', $chans);
    $url="https://api.twitch.tv/helix/streams?user_login=" . $callAPI;
    $ch = curl_init();
  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(

        'Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm',
        'Authorization: Bearer '.$token
  
    ));
    
    $result = curl_exec($ch);
    $i = curl_getinfo($ch);
    curl_close($ch);

    // stampo API
    $str = json_decode($result, true);

```

> [@BarryCarlyon](#):
>
> **DO NOT LEAK YOUR CLIENT SECRET IT IS A PASSWORD**

OPS sorry

---

<div class="post-metadata">

**Author:** ![Emanuele\_Capizzi](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/emanuele_capizzi/32/6915_2.png) [@Emanuele\_Capizzi](https://discuss.dev.twitch.com/u/Emanuele_Capizzi)\
**Post date:** [May 4, 2020, 8:10pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/8 "2020-05-04T20:10:14Z")

</div>

Now work! I’m stupid and i have not updated php.ini and restarted apache2

---

<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 3, 2020, 8:10pm UTC](https://discuss.dev.twitch.com/t/new-api-break-my-old-code/25359/9 "2020-06-03T20:10:21Z")

</div>

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