# I GOT access\_token but i don't know how to use it correctly

**URL:** <https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261>\
**Category:** API\
**Created:** [July 22, 2020, 12:20pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261 "2020-07-22T12:20:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amininano](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/amininano/32/7891_2.png) [@Amininano](https://staging-discuss.dev.twitch.com/u/Amininano)\
**Post date:** [July 22, 2020, 12:20pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/1 "2020-07-22T12:20:31Z")

</div>

![image](https://d2spem0quai2ol.cloudfront.net/original/2X/a/af3c8e93e70ea12a57d26e4ea49318e59e7515ac.png)

* * *

Result :  
Got tokenstdClass Object ( [access\_token] =\> xxxxxxxxxxxxx [expires\_in] =\> 4852593 [token\_type] =\> bearer )

* * *

How to use the acces token to get Streamdata in this code :

function Streamer ($x) {

```
$ch = curl_init('https://api.twitch.tv/helix/users?login=' . $x);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Client-ID: xxxxx'));
$data = curl_exec($ch);
$data = json_decode($data);

var_dump($data);

```

}

It Used to Work for me before but now i don’t know how to include the Acces token in my code , please Help me out , and thank you

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [July 22, 2020, 1:04pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/2 "2020-07-22T13:04:41Z")

</div>

```
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Client-ID: xxxxx'));

```

becomes

```
   curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Client-ID: xxxxx'
        'Authorization: Bearer yyyyyy'
    ));

```

yyyyy being your token

---

<div class="post-metadata">

**Author:** ![Amininano](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/amininano/32/7891_2.png) [@Amininano](https://staging-discuss.dev.twitch.com/u/Amininano)\
**Post date:** [July 22, 2020, 1:20pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/3 "2020-07-22T13:20:32Z")

</div>

**Parse error** : syntax error, unexpected '‘Authorization: Bearer 2m4a77s’ (T\_CONSTANT\_ENCAPSED\_STRING), expecting ‘)’ in

I Got this Error Sir .

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [July 22, 2020, 1:21pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/4 "2020-07-22T13:21:07Z")

</div>

Pre coffee code I missed a `,`

```
   curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Client-ID: xxxxx',
        'Authorization: Bearer yyyyyy'
    ));
```

---

<div class="post-metadata">

**Author:** ![Amininano](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/amininano/32/7891_2.png) [@Amininano](https://staging-discuss.dev.twitch.com/u/Amininano)\
**Post date:** [July 22, 2020, 1:22pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/5 "2020-07-22T13:22:53Z")

</div>

It Worked Now 😃 Thank you So much for the help , and i have one more last question :  
Is It possible to get Acces Token that never expires , so i don’t need to update every time on my website . Thank you again

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [July 22, 2020, 1:23pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/6 "2020-07-22T13:23:41Z")

</div>

> [@Amininano](#):
>
> Is It possible to get Acces Token that never expires , so i don’t need to update every time on my website

No

All tokens expire.

So you should fetch a token, store it then auto refresh/get a new one when the token is near to expiration

---

<div class="post-metadata">

**Author:** ![Amininano](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/amininano/32/7891_2.png) [@Amininano](https://staging-discuss.dev.twitch.com/u/Amininano)\
**Post date:** [July 22, 2020, 1:26pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/7 "2020-07-22T13:26:33Z")

</div>

Got tokenstdClass Object ( [access\_token] =\> xxxxxxxxxx [expires\_in] =\> 5675260 [token\_type] =\> bearer )

Is the Expire value in secondes ?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [July 22, 2020, 1:28pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/8 "2020-07-22T13:28:31Z")

</div>

Correct it’s in seconds.

You can use

> **[Authentication](https://dev.twitch.tv/docs/authentication/)**
>
> Authentication

To check a tokens current expires in time

---

<div class="post-metadata">

**Author:** ![Amininano](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/amininano/32/7891_2.png) [@Amininano](https://staging-discuss.dev.twitch.com/u/Amininano)\
**Post date:** [July 22, 2020, 1:33pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/9 "2020-07-22T13:33:05Z")

</div>

Sorry for Asking a lot Sir but i have another Question :  
On a Website for Each time i need to use an Api and also a Token , do you advice me Requesting a new Token each time or should i just refresh it as said in the twitch docs , which way in your opinion is more optimal .

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [July 22, 2020, 1:34pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/10 "2020-07-22T13:34:42Z")

</div>

Your should generate and store the token using a cron script  
Store the token in a database/flat file/something.

Then your website/whatever will load the token from the database/flat file/something and use it as needed.

Saves wasting a cURL call to generate/check a token each time if you have a separate service managing it

---

<div class="post-metadata">

**Author:** ![Amininano](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/amininano/32/7891_2.png) [@Amininano](https://staging-discuss.dev.twitch.com/u/Amininano)\
**Post date:** [July 22, 2020, 1:37pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/11 "2020-07-22T13:37:46Z")

</div>

Thank you So much Sir for your Time and informations . We are lucky to have you here 😃

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [August 21, 2020, 1:48pm UTC](https://staging-discuss.dev.twitch.com/t/i-got-access-token-but-i-dont-know-how-to-use-it-correctly/27261/12 "2020-08-21T13:48:54Z")

</div>

Closed on August 21, 2020, 1:48pm UTC

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