Webhook subscription accepted but get webhooks endpoint returning empty

/**
 * Subscribes to broadcast changes for the specified broadcaster. If
 * the broadcaster goes offline, handles deletion of trackers.
 * @param {String} broadcasterId Unique of of broadcaster to monitor.
 */
function singleStreamWebhook(broadcasterId){
   
    const topic = encodeURIComponent("https://api.twitch.tv/helix/streams?user_id=" 
            + broadcasterId);
    const callback = encodeURIComponent(CALLBACK_PATH);

    const webhookPath = "/helix/webhooks/hub"
            + `?hub.callback=${callback}`
            + `&hub.mode=subscribe`
            + `&hub.topic=${topic}`
            + `&hub.lease_seconds=864000`
            + `&hub.secret=${SECRET}`;

    const reqOptions = {
        "host": "api.twitch.tv",
        "path": webhookPath,
        "method": "POST",
        "headers": {
            "Client-ID": CLIENT_ID,
        },
    }

    const req = https.request(reqOptions, function(res){

        logger.info(`Webhook request for ${broadcasterId} ended.\n`
                + `\tStatus code: ${res.statusCode}\n`
                + `\tMessage: ${res.statusMessage}`);

    });

    req.on("error", function(err){
        logger.error(`Failed attempt - webhookSubscribe ${broadcasterId}: `
                + `${err.message}`);
    });
    req.end();
}

/**
 * To echo back the challenge.
 */
    else if(req.method == "GET" && webhookPath == CALLBACK){

        const queryParam = req.url.split("?");
        let token = qs.parse(queryParam[1]);
        
        if(token["hub.challenge"] == undefined){
            logger.info(`Subscription mode: ${token["hub.mode"]}`
                    + `Reason: ${token["hub.reason"]}`);
            res.writeHead(json.success);
            res.end();
        }

        else{
            logger.info(`Subscription success: ${queryParam[0]}`);

            // Token will always be at the end of the query string.
            token = token["hub.challenge"];
            res.writeHead(json.success, token);
            res.end();
        }

    }

I’m getting

Status code: 202
Message: Accepted

and I’m always echoing back the challenge token, but when I call

curl -H 'Authorization: Bearer TOKEN' -X GET 'https://api.twitch.tv/helix/webhooks/subscriptions?first=10

I get

{"total":0,"data":[],"pagination":{}}

Anyone have any idea what’s wrong?

This was working a couple of days ago and I didn’t touch this part of my code since then.

Thanks!

This doesn’t seem correct.

You should be using a 200 HTTP code and the token in plain text.

Not a JSON.success

1 Like

Oh, sorry. json.success was a constant in my config file for the 200 HTTP code. I forgot to edit that in my post.

Thank you though. The problem was I wasn’t specifying the content type to be plain text.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.