I have setup a webhoock that listens the “stream changed” webhock endpoint, I’ve setup a cron job to update it every day at midnight.
It was correctly working until yesterday (since 2 month ago more or less), today I’ve received an email telling that my app is using the Helix api without OAuth tokens with are deprecated so I’ve started to migrate all to the new auth method.
The “old” API will be available until the end of this month, so the code I got it should still work.
What I’m trying to find before starting the migration is where the error is, I’ve found that I’m not receiving any callback after the subscribing to the webhoock event (I’m receiving a 202 code with an empty body which is the expected result) but I don’t get anything in the hub.callback
url.
Here’s the resumed code I’m using (this server is using a reverse nginx proxy for https):
const express = require("express");
const request = require("request");
const app = express();
const proxy = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.listen(8000);
app.use("/bot/", proxy);
proxy.get("/", (req, res) => {
if (req.query && req.query["hub.challenge"]) {
console.log("Challenge verification received");
res.set("Content-Type", "text/plain");
res.status(200).send(req.query["hub.challenge"]);
} else {
res.sendStatus(400);
}
});
// webhoock gets here
proxy.post("/", async (req, res) => {
let userId = req.query["user_id"];
let data = req.body.data[0];
// obtain user data given his id
let userInfo = await helper.getUserInfo(userId);
console.log(`Webhoock for user ${userId} (${userInfo.display_name}) obtained.`);
// stop streaming
if (typeof data === "undefined") {
console.log(` User ${userInfo.display_name} is no longer live.`);
// started streaming / updated stream data
} else if (data.type === "live") {
if ((Date.now() - new Date(data.started_at).getTime()) / 1000 <= MAX_LIVE_SINCE) {
console.log(` User ${userInfo.display_name} is live!`);
} else {
console.log(" Stream metadata changed.");
}
}
res.sendStatus(200);
});
And the script I’m using to subscribe to the webhoock event:
const request = require("request");
require('dotenv').config();
const WEBSERVER_ORIGIN = process.env.WEBSERVER_ORIGIN;
const WEBSERVER_BASENAME = `${process.env.WEBSERVER_BASENAME}${(process.env.WEBSERVER_BASENAME.endsWith("/") ? "" : "/")}` || "/";
// Duration of the webhook, maximun value 864000 (10 days)
const LEASE_SECONDS = 24 * 60 * 60;
let baseReq = request.defaults({
baseUrl: "https://api.twitch.tv/helix",
headers: {
"Accept": "application/json",
"Client-ID": process.env.TWITCH_CLIENT_ID
}
});
async function streamsWebhoock(user, unsubscribe) {
return new Promise(async (resolve, reject) => {
if (typeof user === "string") {
// obtain user information given his username
let userInfo = await getUserInfo(user);
user = userInfo.id;
}
if (user) {
baseReq({
uri: "/webhooks/hub",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
"hub.callback": `${WEBSERVER_ORIGIN}${WEBSERVER_BASENAME}?user_id=${user}`,
"hub.mode": (unsubscribe && unsubscribe === true) ? "unsubscribe" : "subscribe",
"hub.topic": `https://api.twitch.tv/helix/streams?user_id=${user}`,
"hub.lease_seconds": LEASE_SECONDS
})
}, (err, res, body) => {
if (err) {
reject(err);
} else if (res && res.statusCode === 202) {
resolve(res, unsubscribe ? "unsuscribe" : "suscribe");
}
});
}
});
}
Edit: I’ve tried to do it with OAuth app tokens and I don’t either recieve any confirmation callbacks. Calling also to subscriptions show no results.