# Invalid OAuth token error

**URL:** <https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573>\
**Category:** API\
**Created:** [May 6, 2021, 7:16am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573 "2021-05-06T07:16:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rouq1567](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rouq1567](https://staging-discuss.dev.twitch.com/u/rouq1567)\
**Post date:** [May 6, 2021, 7:16am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/1 "2021-05-06T07:16:21Z")

</div>

I tried to test the API, but every time I send an import request, I return an error of 401. I don’t know where it went wrong.

{error: “Unauthorized”, status: 401, message: “Invalid OAuth token”}

```
axios.create({
baseURL: 'https://api.twitch.tv/helix',
timeout: 6000,
headers:{
    'Client-ID':'id',
    'Authorization':'Bearer <access token>'
}

```

I got oauth token by referring to this.

GET ‘[https://id.twitch.tv/oauth2/authorize?response\_type=code&client\_id=uo6dggojyb8d6soh92zknwmi5ej1q2&redirect\_uri=http://localhost&scope=viewing\_activity\_read&state=c3ab8aa609ea11e793ae92361f002671](https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=uo6dggojyb8d6soh92zknwmi5ej1q2&redirect_uri=http://localhost&scope=viewing_activity_read&state=c3ab8aa609ea11e793ae92361f002671)’

---

<div class="post-metadata">

**Author:** ![Dist](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/dist/32/7192_2.png) [@Dist](https://staging-discuss.dev.twitch.com/u/Dist)\
**Post date:** [May 6, 2021, 7:54am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/2 "2021-05-06T07:54:09Z")

</div>

> [@rouq1567](#):
>
> GET ‘[https://id.twitch.tv/oauth2/authorize?response\_type=code&client\_id=uo6dggojyb8d6soh92zknwmi5ej1q2&redirect\_uri=http://localhost&scope=viewing\_activity\_read&state=c3ab8aa609ea11e793ae92361f002671’](https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=uo6dggojyb8d6soh92zknwmi5ej1q2&redirect_uri=http://localhost&scope=viewing_activity_read&state=c3ab8aa609ea11e793ae92361f002671%E2%80%99)

Once you get the code from that auth flow, do you follow the the rest of the require steps explained in the documentation to exchange that code for an Access Token? [Getting OAuth Access Tokens | Twitch Developers](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow)

---

<div class="post-metadata">

**Author:** ![rouq1567](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rouq1567](https://staging-discuss.dev.twitch.com/u/rouq1567)\
**Post date:** [May 6, 2021, 8:23am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/3 "2021-05-06T08:23:20Z")

</div>

Yes, I changed the client\_id part to my client ID and got an access token. I did it the same way two months ago, but there was no problem then.

---

<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:** [May 6, 2021, 10:04am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/4 "2021-05-06T10:04:21Z")

</div>

Did you exchange the returned ?code for an access token as per step 3?

```auto
3) On your server, get an access token by making this request:

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>
Here is a sample request:

POST https://id.twitch.tv/oauth2/token
    ?client_id=uo6dggojyb8d6soh92zknwmi5ej1q2
    &client_secret=nyo51xcdrerl8z9m56w9w6wg
    &code=394a8bc98028f39660e53025de824134fb46313
    &grant_type=authorization_code
    &redirect_uri=http://localhost

```

On the docs Dist Linked?

---

<div class="post-metadata">

**Author:** ![rouq1567](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rouq1567](https://staging-discuss.dev.twitch.com/u/rouq1567)\
**Post date:** [May 6, 2021, 11:06am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/5 "2021-05-06T11:06:48Z")

</div>

Aha, I haven’t done phase three so far. Thanks to you, you received a refresh token and an access token! But the status is still 401, what should I do to use the API freely?

---

<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:** [May 6, 2021, 11:08am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/6 "2021-05-06T11:08:09Z")

</div>

Whats the body message attached to the 401?

---

<div class="post-metadata">

**Author:** ![rouq1567](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rouq1567](https://staging-discuss.dev.twitch.com/u/rouq1567)\
**Post date:** [May 6, 2021, 11:13am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/7 "2021-05-06T11:13:52Z")

</div>

Here’s what the message is about.

```
{
    "status": 400,
    "message": "Invalid authorization code"
}

```

Is it possible to request a request only once? I sent the request several times because I didn’t know well.

---

<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:** [May 6, 2021, 11:14am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/8 "2021-05-06T11:14:24Z")

</div>

That suggests you tried to reuse the ?code to get an access token.

A ?code can only be exchanged for an access token once.

---

<div class="post-metadata">

**Author:** ![rouq1567](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rouq1567](https://staging-discuss.dev.twitch.com/u/rouq1567)\
**Post date:** [May 6, 2021, 11:36am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/9 "2021-05-06T11:36:37Z")

</div>

Fortunately, I sent the request from the beginning and received the token in the form of JSON. By the way, how can I use the API using the token I received now?  
The status of 401 is the result of the api requested by axios.get().

Below is the response I received after running step 3.

```
{
    "access_token": "access_token",
    "expires_in": 14376,
    "refresh_token": "refresh_token",
    "scope": [
        "viewing_activity_read"
    ],
    "token_type": "bearer"
}
```

---

<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:** [May 6, 2021, 11:42am UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/10 "2021-05-06T11:42:48Z")

</div>

> [@rouq1567](#):
>
> By the way, how can I use the API using the token I received now?

You have already posted the answer:

> [@rouq1567](#):
>
> ```auto
> axios.create({
> baseURL: 'https://api.twitch.tv/helix',
> timeout: 6000,
> headers:{
> 'Client-ID':'id',
> 'Authorization':'Bearer <access token>'
> }
> 
> ```

Step 3 you exchange the ?code for a token.  
Then you take the token from the JSON blob and use that in API requests

---

<div class="post-metadata">

**Author:** ![rouq1567](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rouq1567](https://staging-discuss.dev.twitch.com/u/rouq1567)\
**Post date:** [May 6, 2021, 12:13pm UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/11 "2021-05-06T12:13:15Z")

</div>

I put in the access token and it works well. I think I know what the problem was now. I think it was because I didn’t go through the middle stage. Thank you!

---

<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:** [June 5, 2021, 12:14pm UTC](https://staging-discuss.dev.twitch.com/t/invalid-oauth-token-error/31573/12 "2021-06-05T12:14:09Z")

</div>

Closed on June 5, 2021, 12:14pm UTC

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