Hello, I’m trying this in node and somehow am stuck as well. Any suggestions? I always get {“error”:“Unauthorized”,“status”:401,“message”:“authentication failed”} returned to me.
const currentSecret = '<base64 encoded secret copied from extension settings>';
const ChannelId = '<my channel id>';
const OwnerId = '<my user id>';
const ClientId = '<client id from overview on my extension>';
const jwt = require('jsonwebtoken');
const request = require("request");
const secret = Buffer.from(currentSecret, 'base64');
const signedJwt = makeServerToken(ChannelId);
var options = {
url : 'https://api.twitch.tv/extensions/message/' + ChannelId,
headers :{
'Authorization' : 'Bearer ' + signedJwt,
'Client-Id': '<my client id from extensions overview>',
'Content-Type': 'application/json'
},
body : JSON.stringify({
message: { "foo":"bar" },
content_type: 'application/json',
targets: ['broadcast']
})
};
function details(error,response,body) {
console.log(body);
}
request(options, details);
// Create and return a JWT for use by this service.
function makeServerToken(channelId) {
const payload = {
exp: Math.floor(Date.now() / 1000) + 60,
channel_id: String(channelId),
role: 'external',
pubsub_perms: {
send: ['broadcast'],
},
};
return jwt.sign(payload, secret, { algorithm: 'HS256' });
}