Hello dev Twitch community,
I’m new w/ Twitch API & NodeJS.
I tried to suscribe to streams webhooks but I didn’t receive any data.
I had the 202 Accepted response and I got the challenge. I display the challenge on page,
but I don’t receive any data. I have wait for more than 5 minutes and nothing appear.
My process should be wrong. Someone to help me to understand where I’m wrong & where I had to handle data ?
Thanks
// Modules declaration
const express = require('express')
const app = express()
const request = require('request')
// Send Twitch webhooks subscription
// Twitch client id
var clientID = 'clientidvalue'
// Content type of data to send
var contentType = 'application/json'
// Twitch URI to contact
var postURI = 'https://api.twitch.tv/helix/webhooks/hub'
// Secret phrase to authenticate response
var secret = 'secretvalue'
// Multiple params to send to Twitch
var options = {
// Array w/ all headers params
headers: {
// Twitch client id
'Client-ID': clientID,
// Content type of data to send
'Content-Type': contentType
},
// Twitch URI to contact
uri: postURI,
// Method use to send our request
method: 'POST',
json: {
"hub.mode": "subscribe",
"hub.topic": "https://api.twitch.tv/helix/streams?user_id=64989126",
"hub.callback": "https://api.gius.tv/twitch/streams",
"hub.lease_seconds": 864000,
"hub.secret": secret
}
}
request(options, function (error, response, body) {
console.log('Request status code : ' + response.statusCode)
if (response.statusCode === 202) {
console.log(response.statusCode + ' ' + response.statusMessage)
} else {
console.log(response.statusCode + ' ' + response.statusMessage)
}
})
// Routes
// Homepage route
app.get('/', function(req, res) {
res.send('Site en maintenance. #Scusate :D')
})
// Callbacks handlers
// Streams Up/Down
app.get('/twitch/streams', function (req, res) {
res.send(req.query['hub.challenge'])
console.log(req.query)
console.log(req.headers)
//console.log(res)
/*var incoming = req.headers['x-hub-signature'].split('=');
var hash = crypto.createHmac(incoming[0], secret)
.update(JSON.stringify(req.body))
.digest('hex')
if (incoming[1] != hash) {
console.log('Reject')
} else {
console.log('Payload OK')// do other stuff
}*/
})