Difficulties with Oauth in HTTP Request for Twitch

I’ve been using n8n to try automating some tasks with regards to Twitch. One is to try seeing when a user goes live and stops although I want to poll for other metadata as well with HTTP requests. I’m using a workflow like this:

It does the above-stated polling and then posts to a webhook for Discord only when the user goes live and stops streaming. This appears to work for about 5 hours before I get an error 400 from the Twitch API. The only way for me to resolve this is by doing the OAuth connection again. Error 400 appears to be related to a bad request with the refresh token according to the Twitch API documents.

Some other details for using OAuth with Twitch is that it requires that the credentials be sent in the body. This seems to be a little similar to this issue although it was with Hubspot.

I’m wondering how I might be able to resolve this so that I don’t have to manually go in to fix the OAuth issue every 5~ hours?

Hey @michaelnguyen!

Welcome to the community :tada:

Is there any particular reason you’re use Cron node to poll? I have been using the EventSub API to trigger the workflow when I go live. I haven’t faced any issues with the credentials for the same.

I also see that you’re using the HTTP Request node to Post a message to your Discord server. You can use the Discord node to do the same.

Ah that is certainly related. Will try to figure that out this week. In the meantime you can use what @harshil1712 suggested and the webhook node.

Admittedly I don’t know how to set up Twitch with the webhook as well so I just have this set up right now. If you happen to know the exact information/node workflow to get that running I would greatly appreciate it.

The other reason for polling though is that eventsub doesn’t really provide information on viewer numbers so since I was going to poll anyway, I was going to grab those numbers. Unless I’m missing something here.

Lastly, I post a message to discord because currently, as far as I know, the Discord node doesn’t support embeds, only text. This is not the official documentation but you can see examples better here. Official doc for discord webhook embeds.

Hey!

Yes, EventSub unfortunately doesn’t provide the viewer count. I will try to look into a better way to do this. I have quickly created a short draft of the steps that you can follow to connect Twitch with the Webhook node.

  1. Create a Twitch app on the developer console.
  2. You can add http:localhost:3000 as the OAuth redirect.
  3. Install the Twitch CLI. We need to get this to get the App Access Token. (This is the easiest way to get the tokens, in my experience. If you have a simpler way, I would love to learn that!)
  4. Execute twitch configure command. Enter the Client ID and the Client Secret that got generated after you created the Twitch app (Step 1 and 2).
  5. I used Postman to make a POST request to register my Webhook. You can use a similar tool or the cURL command.
  6. Make a post request to https://api.twitch.tv/helix/eventsub/subscriptions. You need to pass on Client ID and Authorization in the headers. The value for Authorization will be similar to Bearer APP_ACCESS_TOKEN.
  7. Use the following JSON in the Body of the request
{
    "type": "stream.online", // the type you want to subscribe to. You can find more about the types on their documentation
    "version": "1",
    "condition": {
        "broadcaster_user_id": "YOUR BROADCASTER ID" // you can get this by using the command mentioned below
    },
    "transport": {
        "method": "webhook",
        "callback":  "YOUR WEBHOOK URL", // you will get this from the Webhook node
        "secret": "SECRET" // has to be min 10 characters
    }
}

Get Broadcaster ID:
Execute the command twitch api get users -q login=harshil1712 (Replace harshil1712 with your Twitch username).

Let me know if you face any issues with the steps mentioned above. I am working on a blog-post and will try to publish it by Sunday :slight_smile:

2 Likes

I was able to get an event following the above instructions in conjunction with verifying the webhook from the instructions harshil1712 had here, after modifying the Webhook from GET to POST. I appreciate the help, thank you.

Hope the OAuth issue isn’t too difficult of a fix as it seems that if I want certain other info like the title/game the moment a user goes live, I’ll have to make an HTTP request and the token will expire at some point again.

Hey @michaelnguyen!

I wrote a tutorial that shows the step to connect Twitch with n8n. You can read it here: Automate Tweets for your Twitch Live Stream using n8n - Part 1

2 Likes

@harshil1712 the tutorial looks great! In fact, I learned some more stuff I didn’t realize were available for testing like Twitch mock events. Thanks a bunch!

1 Like