Event Sub Example Not Working

Hi there!

I’m using the example produced by Barry:

Event Sub PHP

and I’m able to submit the subscription request, and receive the notification that it’s pending, but after that, things just stop.

Looking at the log Barry set up, it reads:

“Starting a POST processing
Missing sig”

And then it stops.

I’ve enabled the body data log, and can see that the following is being sent:

{“subscription”:{“id”:“CODE”,“status”:“webhook_callback_verification_pending”,“type”:“channel.follow”,“version”:“1”,“condition”:{“broadcaster_user_id”:“417634528”},“transport”:{“method”:“webhook”,“callback”:“https://WEBSITE.com/SUBDOMAIN/index.php"},“created_at”:“2021-07-27T16:46:56.928500273Z”,“cost”:0},“challenge”:"RANDOM STUFF”}

In the Head Data log, I’m seeing the following:

[Twitch-Eventsub-Subscription-Version] => 1
[Twitch-Eventsub-Subscription-Type] => channel.follow
[Twitch-Eventsub-Subscription-Is-Batching-Enabled] => false
[Twitch-Eventsub-Message-Type] => webhook_callback_verification
[Twitch-Eventsub-Message-Timestamp] => 2021-07-27T16:46:56.934171005Z
[Twitch-Eventsub-Message-Signature] => sha256=RANDOM STUFF
[Twitch-Eventsub-Message-Retry] => 3
[Twitch-Eventsub-Message-Id] => RANDOM STUFF
[Content-Type] => application/json

Can anyone see what I’m missing?
I know it’s reaching the site, as the log is being generated. It’s just failing to find the signature.

Also, in the PHP, it’s showing:

“echo rawurlencode($_GET[‘hub_challenge’]);”

Should it be hub_challenge?

I’m not familiar with PHP, but I feel I’m so close to finally getting Eventsub working!

Looking into the code a bit more, I noticed that a lot of it was lowercase… for example, instead of “Twitch-Eventsub-Message-Signature” it was “twitch-eventsub-message-signature”.

Correcting this seems to let me get further along, and I can see the two signatures match, but it’s not sending back the challenge. I wonder if this part is incorrect, too.

My example I moved up from webhooks and just fixed the signature logic.

So yeah theres a mistake.

Should be

        if ($headers['twitch-eventsub-message-type'] == 'webhook_callback_verification') {
            // it's a verification request
            $data = json_decode($raw_data);
            // and check the data parse
            if (json_last_error() == JSON_ERROR_NONE) {
                echo rawurlencode($data['challenge']);
                exit;
            }
            echo 'Failed to parse JSON';
            exit;
        }

I’ll update the file shortly.

I don’t use the PHP version in production :stuck_out_tongue: so it’s just an example for the sig logic.

According to the spec all headers (the HTTP Speci not EventSub spec) should be lower case internally/treated as lowercase. Seems PHP is leaving them as whatever they are in bound when they arrive. (Which is different in other languages)

I’ll add a fix to the logic

File revised

Lemme know how you get on

Thanks for that!

Strangely enough, however… and I’m not sure why, but when I access the new index.php it doesn’t even produce the log anymore. Usually, it would produce the log right up until there was an error, then it would simply stop.

I renamed my old file, then did a copy/paste of your code and have ensure that my code is directed toward the new php.

Honestly, I can’t see what would stop it producing any of the logs.

Just changed my code to read the old php file, and the logs were generated again.

Errr

Parse error: syntax error, unexpected token “:” in /Users/barrycarlyon/Sites/github/twitch_misc/eventsub/handlers/php/index.php on line 84

That would be the syntax error on line 84 :smiley:

Yup! That was it :slight_smile:
Now, I appear to be getting mismatch regarding the sigs. Is the process of converting all of the headers to lowercase somehow altering the signatures, so they no longer match?

No coz it only changes the key in the header not the contents of the header.

But if I go back to the old php, they match again.

Line 65 is wrong

$twitch_event_timestamp = $headers[‘twitch-eventsub-message-timestam’];

should be

$twitch_event_timestamp = $headers[‘twitch-eventsub-message-timestamp’];

That appears to be the closest so far, but it’s unfortunately sat on pending.

It doesn’t appear to be sending back that challenge.

I added “mylog(‘Success!!!’);” to the log so I can see if it got through, but I’m not seeing the “Returning the challenge” log entry.

if ($our_signature == $twitch_signature) {
    // passes test
mylog('Success!!!');
    if ($headers['twitch-eventsub-message-type'] == 'webhook_callback_verification') {
        // it's a verification request
        $data = json_decode($raw_data);
        // and check the data parse
        if (json_last_error() == JSON_ERROR_NONE) {
            mylog('Returning the challenge: ' . $data['challenge']); 
            echo rawurlencode($data['challenge']);
            exit;
        }
        mylog('Failed to parse the JSON to verification');
        echo 'Failed to parse JSON';
        exit;
    }

Would that suggest it IS finding an error while decoding the JSON?

            if (json_last_error() == JSON_ERROR_NONE) {
                mylog('Returning the challenge: ' . $data->challenge);
                echo rawurlencode($data->challenge);
                exit;
            }

Forgot that json_decode makes a object not an array by default

tested the script with TwitchCLI both to verify and recv a payload and it seems to be behaving as required now

That did it! Brilliant! Thank you so much!

Hi @ BarryCarlyon,
I’ve looked at your example, as well as many others in online.

So glad to see I’m not the only one with this issue. Meaning, responding to Twitch’s ack of our subscription. Btw, I’m not working in PHP.
When I run the request I get a 404.

I’m looking for a simple request example which specifically states:

  1. Twitch Endpoint
  2. URL Parameter(s)
  3. Headers
  4. Body

Here is what I’m doing:

  1. Endpoint - https://api.twitch.tv/helix/webhooks/callback
  2. URL Parameter - ?status=200
  3. Headers - Client-ID & Authorization
  4. Body - challenge (as a string in raw format)

Please let me know where I went wrong. Thank you in advance!

You get a 404 because that endpoint doesn’t exist.

You seem to be trying to create a new request, when all you need to do is respond to Twtich’s request.

For the Callback verification you respond with the challenge and a 200 status, and for notification responses you just respond with a 200.

Don’t do this.

You need to reply to it like you would a website login to your website.

  • Someone goes to a page
  • Fills in the login form
  • The users hits login
  • You reply “password ok” or “password invalid”

This works simarily in eventsub

  • You Tell Twitch to make a subscription
  • Twitch fills in a form
  • Twitch hits go
  • You reply with the challenge (from the form) or reject it

In neither of these examples do you make a “second” request to an API.

Can you please give me an example of how you reply with the challenge?
Thanks!

Though this is language depending.

Basically however you output HTML for a webpage in your language of choice

Thank you Barry! I really appreciate your help. I’m a beginner programer in high school.
I understand that we need to reply back to Twitch w/ the challenge. I need some details.
I don’t understand the method of communication.
Also, I don’t know that language. I’m using Go if that helps.

Nevermind, I think I get it. We need to send the challenge in the response to the webhook.
If I’m wrong, please let me know. Thanks again!