# Invalid authorization code nodejs

**URL:** <https://discuss.dev.twitch.com/t/invalid-authorization-code-nodejs/21596>\
**Category:** API\
**Created:** [June 22, 2019, 6:31pm UTC](https://discuss.dev.twitch.com/t/invalid-authorization-code-nodejs/21596 "2019-06-22T18:31:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![D4rkcide](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/d4rkcide/32/6526_2.png) [@D4rkcide](https://discuss.dev.twitch.com/u/D4rkcide)\
**Post date:** [June 22, 2019, 6:31pm UTC](https://discuss.dev.twitch.com/t/invalid-authorization-code-nodejs/21596/1 "2019-06-22T18:31:55Z")

</div>

I added some error handling to my code now every request I send to twitch is returning `400 invalid authorization code`.The authorization code always has a value that ‘looks’ legit, but still no dice. If anyone can take a look I would really appreciate it. here is the relevant code

in my server.js:

```auto
    if (!req.query.code) throw new Error('Error authorization code is null');
    try {
        var response = await twitchOAuth.getToken(req.query.code);
        twitchId = twitchOAuth.decodeToken(response);
        res.cookie('twitchId', twitchId);
        res.redirect(baseUrl);
    } catch (err) {
        log.info(err, 'twitch');
        res.redirect('/error');
    }
});

//this is where the error is happening:
const getToken = async(code) => {
    try {
        if (!code) throw new Error('parameter code is null');
        const response = await fetch(`https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${clientSecret}&code=${code}&grant_type=authorization_code&redirect_uri=${redirectUri}/twitchcallback`, {
            method: 'POST',
        });
        return await response.json();
    } catch (err) {
        throw new Error(`Error getting token: ${JSON.stringify(err)}`);
    }
};

//the method failing because of the error:
const decodeToken = (response) => {
    try {
        if (!response.id_token) throw new Error(' Parameter response.id_token is null');
        var decoded = jwt.decode(response.id_token, { complete: true });
        return decoded.payload.sub; //this is the twitchId
    } catch (err) {
        throw new Error(`Error decoding token: ${JSON.stringify(err)}`);
    }
};
```

---

<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:** [June 23, 2019, 12:10am UTC](https://discuss.dev.twitch.com/t/invalid-authorization-code-nodejs/21596/2 "2019-06-23T00:10:40Z")

</div>

Your getToken appears to be correct.

So we should check your redirect to Twitch code/rest of the code.

---

<div class="post-metadata">

**Author:** ![D4rkcide](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/d4rkcide/32/6526_2.png) [@D4rkcide](https://discuss.dev.twitch.com/u/D4rkcide)\
**Post date:** [June 26, 2019, 4:34am UTC](https://discuss.dev.twitch.com/t/invalid-authorization-code-nodejs/21596/3 "2019-06-26T04:34:07Z")

</div>

I was actually using the clientId of another app, that was my issue

---

<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:** [July 26, 2019, 4:34am UTC](https://discuss.dev.twitch.com/t/invalid-authorization-code-nodejs/21596/4 "2019-07-26T04:34:09Z")

</div>

Closed on July 26, 2019, 4:34am UTC

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