Using Node.js and jwt-simple, getting a 40 unauthorized error. Checked the token on 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’,
port: 443,
path: ‘/extensions/myclientidlongversion/auth/secret’,
method: ‘GET’,
headers: {
‘Authorization’: 'Bearer ’ +token,
‘Client-Id’: ‘myclientidlongversion’
}
};
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 instead 
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.
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.
1 Like
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!
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?
After really hitting this from all angles, I actually found my solution in one of your comments from another post on here 
var secret = Buffer.from('sOmESecReT=', 'base64');
That line saved my butt, as I’m now getting the results I needed. Thank you again!