# Refresh token auto renewal

**URL:** <https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587>\
**Category:** API\
**Created:** [October 12, 2020, 12:27pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587 "2020-10-12T12:27:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hazem\_Elsawy](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/hazem_elsawy/32/10445_2.png) [@Hazem\_Elsawy](https://staging-discuss.dev.twitch.com/u/Hazem_Elsawy)\
**Post date:** [October 12, 2020, 12:27pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/1 "2020-10-12T12:27:41Z")

</div>

Hello,

I am trying to renew the refresh token,

When it expires, I need to go through

1- Get “code” by creating this request

```auto
GET 'https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=uo6dggojyb8d6soh92zknwmi5ej1q2&redirect_uri=http://localhost&scope=viewing_activity_read&state=c3ab8aa609ea11e793ae92361f002671'

```

2- Get refresh token by creating this request

```auto
POST https://id.twitch.tv/oauth2/token
    ?client_id=<your client ID>
    &client_secret=<your client secret>
    &code=<authorization code received above>
    &grant_type=authorization_code
    &redirect_uri=<your registered redirect URI>

```

3- I keep renewing the access token by

```auto

https://id.twitch.tv/oauth2/token?grant_type=refresh_token&refresh_token=xxxx&client_id=xxxx&client_secret=xxxx

```

and I keep storing the refresh token that is returned along with access token for reuse.

4- after a couple of months, the refresh token is no longer valid, and I need to repeat these steps again.

so, is there a good way to automate this?,

for example, I’m trying to take authentication GET request out of these steps to be able to automate it.

Any help will be greatly appreciated,

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:** [October 12, 2020, 12:30pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/2 "2020-10-12T12:30:17Z")

</div>

> [@Hazem\_Elsawy](#):
>
> 4- after a couple of months, the refresh token is no longer valid, and I need to repeat these steps again.

Something else occurred to invalidate the set of tokens.

Assuming you loop step 3 when the token dies.

Then you have something else going on killing the token set.

As what you are doing is correct

---

<div class="post-metadata">

**Author:** ![Hazem\_Elsawy](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/hazem_elsawy/32/10445_2.png) [@Hazem\_Elsawy](https://staging-discuss.dev.twitch.com/u/Hazem_Elsawy)\
**Post date:** [October 12, 2020, 12:45pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/3 "2020-10-12T12:45:44Z")

</div>

Thank you for your response,

My guess was that, in between the period it takes to renew the access token, the refresh token expired,

I try to show what I think in the following example :

1-All good \_\_\_\_\_\_\_\_\_\_2- All good\_\_\_\_\_\_\_\_\_\_3- All good\_\_\_\_RTexpired\_\_\_4- “Invalid refresh token” ( not all good anymore )

Do refresh tokens expire that way ?

Cheers

---

<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:** [October 12, 2020, 12:48pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/4 "2020-10-12T12:48:06Z")

</div>

Refresh Tokens don’t have an expiry attached to them.

> [@Hazem\_Elsawy](#):
>
> and I keep storing the refresh token that is returned along with access token for reuse.

You already said you are storing the new refresh token.

So you have _something else_ going on that is causing the token and it’s refresh token to be dead.

As you should be able to keep refreshing forever!

Either the user did something like

- disconnect the app,
- reset their password,
- generated 25 tokens, and the 26th token killed the first in the list. (Generating could be logging into your application for example, and not revoking as you go)

(Generating 25 tokens doesn’t mean refreshing, it’s 25 token/refresh token pairs)

---

<div class="post-metadata">

**Author:** ![Hazem\_Elsawy](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/hazem_elsawy/32/10445_2.png) [@Hazem\_Elsawy](https://staging-discuss.dev.twitch.com/u/Hazem_Elsawy)\
**Post date:** [October 12, 2020, 12:53pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/5 "2020-10-12T12:53:07Z")

</div>

That’s brilliant,

```auto
POST
https://id.twitch.tv/oauth2/token?grant_type=refresh_token&refresh_token=xxxx&client_id=xxxx&client_secret=xxxx

```

By making this request I get the same refresh token each time ( I send refresh\_token=xxxx and I get refresh\_token=xxxx ), and that’s where I store it, it doesn’t make sense to me to store it since it’s the same each time, but I read that I may get a different one so I need to store it again and again.

If the refresh token I send/get at this request does not expire, then as you said there must be something else that happened.

Many thanks

---

<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:** [October 12, 2020, 12:53pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/6 "2020-10-12T12:53:52Z")

</div>

> [@Hazem\_Elsawy](#):
>
> By making this request I get the same refresh token each time ( I send refresh\_token=xxxx and I get refresh\_token=xxxx ), and that’s where I store it, it doesn’t make sense to me to store it since it’s the same each time, but I read that I may get a different one so I need to store it again and again.

Yup, you _might_ get a new one, you might not, so I always grab and store myself

---

<div class="post-metadata">

**Author:** ![Hazem\_Elsawy](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/hazem_elsawy/32/10445_2.png) [@Hazem\_Elsawy](https://staging-discuss.dev.twitch.com/u/Hazem_Elsawy)\
**Post date:** [October 12, 2020, 12:57pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/7 "2020-10-12T12:57:46Z")

</div>

Okay, that sounds great.

Many thanks, much appreciated.

---

<div class="post-metadata">

**Author:** ![Hazem\_Elsawy](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/hazem_elsawy/32/10445_2.png) [@Hazem\_Elsawy](https://staging-discuss.dev.twitch.com/u/Hazem_Elsawy)\
**Post date:** [October 12, 2020, 1:24pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/8 "2020-10-12T13:24:20Z")

</div>

I have just noticed, we changed the password around the time the error happened, do you think this maybe the reason ?

---

<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:** [October 12, 2020, 1:25pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/9 "2020-10-12T13:25:15Z")

</div>

yup password resets _can_ cause app dissconnections.

Usually thats more “password reset routine” than a regular reset.

But it’s been known to occur

---

<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:** [November 11, 2020, 1:25pm UTC](https://staging-discuss.dev.twitch.com/t/refresh-token-auto-renewal/28587/10 "2020-11-11T13:25:16Z")

</div>

Closed on November 11, 2020, 1:25pm UTC

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