# JWT 401 Error using Node.js & jwt-simple

**URL:** <https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182>\
**Category:** Extensions\
**Created:** [December 5, 2017, 3:04pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182 "2017-12-05T15:04:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Lavoie](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/joe_lavoie/32/3775_2.png) [@Joe\_Lavoie](https://discuss.dev.twitch.com/u/Joe_Lavoie)\
**Post date:** [December 5, 2017, 3:04pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/1 "2017-12-05T15:04:39Z")

</div>

Using Node.js and jwt-simple, getting a 40 unauthorized error. Checked the token on [jwt.io](http://jwt.io), and it verifies the signature. Not sure what I’m doing wrong. Any thoughts?

> var d = new Date();  
> var seconds = Math.round(d.getTime() / 1000);  
> var epoch = seconds + 60;
> 
> var payload = {‘exp’: epoch, ‘user\_id’: ‘mychannelid’, ‘role’: ‘external’};  
> var secret = ‘myextensionssecret’;
> 
> var token = jwt.encode(payload, secret);
> 
> var options = {  
> host: ‘[api.twitch.tv](http://api.twitch.tv)’,  
> port: 443,  
> path: ‘/extensions/myclientidlongversion/auth/secret’,  
> method: ‘GET’,  
> headers: {  
> ‘Authorization’: 'Bearer ’ +token,  
> ‘Client-Id’: ‘myclientidlongversion’
> 
> ```
> }
> 
> ```
> 
> };

---

<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:** [December 5, 2017, 4:08pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/2 "2017-12-05T16:08:09Z")

</div>

I use this which works:

```
var tokenPayload = {
    exp: Math.floor(new Date().getTime() / 1000) + 60,
    channel_id: ''+channel,
    role: 'external',

    pubsub_perms: {
        send: [
            'broadcast'
        ]
    }
}

```

Side note I’m using [https://www.npmjs.com/package/jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken) instead 😃

---

<div class="post-metadata">

**Author:** ![Joe\_Lavoie](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/joe_lavoie/32/3775_2.png) [@Joe\_Lavoie](https://discuss.dev.twitch.com/u/Joe_Lavoie)\
**Post date:** [December 5, 2017, 4:38pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/3 "2017-12-05T16:38:16Z")

</div>

Modified my code using yours. Same error. Wondering if it’s in the headers being sent? Not sure if I’m doing that right. Verification of the token works. Hmm.

Thank you for your help, though.

---

<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:** [December 5, 2017, 6:31pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/4 "2017-12-05T18:31:47Z")

</div>

```
var tokenPayload = {
    exp: Math.floor(new Date().getTime() / 1000) + 60,
    channel_id: ''+channel,
    role: 'external',

    pubsub_perms: {
        send: [
            'broadcast'
        ]
    }
}

let signedJwt = jwt.sign(tokenPayload, secret);

request.post({
    url: 'https://api.twitch.tv/v5/extensions/message/' + channel,
    headers: {
        Accept: 'application/vnd.twitchtv.v5+json',
        Authorization: 'Bearer ' + signedJwt,
        'Client-ID': client_id,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        message: payload,
        content_type: 'application/json',
        targets: ['broadcast']
    }),
    gzip: true
}, function(e, r, b) {
    if (e) {
        console.log(e);
    } else if (r.statusCode == 204) {
        console.log('Ok to ' + channel + ' with ' + mode + ' Left ' + stack[channel].length);
    } else {
        console.log('Got ' + r.statusCode + ' to ' + channel + ' with ' + mode);
        console.log(b);
    }
});

```

Does this help?

Response is 204 if all good.

---

<div class="post-metadata">

**Author:** ![Joe\_Lavoie](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/joe_lavoie/32/3775_2.png) [@Joe\_Lavoie](https://discuss.dev.twitch.com/u/Joe_Lavoie)\
**Post date:** [December 5, 2017, 6:56pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/5 "2017-12-05T18:56:00Z")

</div>

Just using your code, I get a 400, missing required parameter “message”.

At least not a 401. What should payload (not tokenPayload) be there?

Thanks again!

---

<div class="post-metadata">

**Author:** ![Joe\_Lavoie](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/joe_lavoie/32/3775_2.png) [@Joe\_Lavoie](https://discuss.dev.twitch.com/u/Joe_Lavoie)\
**Post date:** [December 5, 2017, 8:58pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/6 "2017-12-05T20:58:14Z")

</div>

Nevermind, I think what’s happening is I’m confused on the secret. What secret am I using, and do I have to decode it before generating the token?

---

<div class="post-metadata">

**Author:** ![Joe\_Lavoie](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/joe_lavoie/32/3775_2.png) [@Joe\_Lavoie](https://discuss.dev.twitch.com/u/Joe_Lavoie)\
**Post date:** [December 5, 2017, 9:15pm UTC](https://discuss.dev.twitch.com/t/jwt-401-error-using-node-js-jwt-simple/13182/7 "2017-12-05T21:15:50Z")

</div>

After really hitting this from all angles, I actually found my solution in one of your comments from another post on here 😃

> [@\[SOLVED\] Can't Set Extension Required Configuration](https://discuss.dev.twitch.com/t/solved-cant-set-extension-required-configuration/11880/4):
>
> I quickly looked at the readme, try passing new Buffer('extension\_SecReT=', 'base64') as secret. It doesn’t say anywhere on the sign doc, but on the verify doc [https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback)

`var secret = Buffer.from('sOmESecReT=', 'base64');`

That line saved my butt, as I’m now getting the results I needed. Thank you again!
