Understanding token auth via python

Hello - Decided I wanted a simple Discord bot and learned how to write a VERY simple bot as my first coding project. This lead me to wanting to integrate a notification message system for when specific streamers come online. This is my first attempt at interacting with another API.

As I was doing some research I happened upon a python script to request the data of specific streamers by authorizing the request with your token. It was actually written by a mod here - Barry Carlyon. As I’m still learning I’m currently working my way through the simple request and understanding what exactly is going on and I need a bit of assistance to understand a few things.

The information I’m requesting requires via OAuth or App Access Token. Whenever you request a specific streamers information it appears YOUR token changes upon each request. Reading through the documentation it reads, “The grant request below requires the client secret to acquire an app access token; this also should be done only as a server-to-server request, never in client code.”

Now if I wanted to write a script that automatically sends a message to my discord channel when “x” streamer comes online then that would mean I’d be generating a token every “x” times I make that request. This seems to violate the documentation as it’s in my code.

Am I understanding this correctly? Should this type of request never be on a “x” time loop? Thank you for any guidance

You refer to

yeah?

The code just tells you how to do it.
But it doesn’t give an opinion on where/how to store a token.

The general flow for a “production” script is:

  • Script starts
  • Script sees if it has a token in storage
    • If existing token exists in storage, load that token from storage
  • if the token doesn’t exist or has expired
    • generate a token
    • Store that token
  • use token to get stream status

However, for a Discord bot doing live streamer status.

You potentitally want to use EventSub instead

Then Twitch will notify you when a stream starts (or ends).
Rather than you needing to poke the API every few minutes.

Then you only generate an use an app access token when creating eventsub subscriptions.
So you’d set it up once and thats it. No more token schnanigans unless you need to remake the exsitong or make new subscriptions to new streamers live status.

However,

If you ONLY have one script total. Then always generating an App Access Token at script start is ok. As you only have the one process and won’t run into the 50 token rule.

Ah ok - I misread the 50 token rule and had thought it was 50 tokens total not per request. I thought there might be an issue with requesting a new token so frequently. That’s good. This gives me a bit of practice simply writing the script into a loop and just playing around before I attempt to tackle the eventsub subscription.

The eventsub is a bit out of my element but I’m OK with learning it. Does the discord webhook handle the SSL and port requirements? Is there anything you could provide for someone new like me to learn more about that?

You can’t connect a Twitch Eventsub directly to a Discord Webhook.

You need the Twitch Eventsub to hit your server.
Then you process the data and forward to a Discord Webhook

It’s not directly compatible.

I don’t ahve any Python resources for EventSub. I did write a whole nodeJS/live example

The “logic” is comparabel to swap over to python.

Could that server be a Google Cloud VM? As of right now I have my bot script running 24/7 via pm2 on a Google Cloud VM.

This is a lot of information to parse through and to understand. I will need time to learn this. To break it down this is simply monitoring a streamers information, notifies my server when whatever event I subscribe to happens, then sends that information via discords webhook to my script?

Sure

No not via discords webhook to my script?

Like this:

You generate an app access token to use Getting Tokens: OAuth | Twitch Developers

You want to know when lirik goes live
You get lirik's user ID from get Users Reference | Twitch Developers

You create an eventsub notification handler which is a SSL’ed URL somewhere you control

You subscribe to stream.online using liriks userID EventSub Subscription Types | Twitch Developers - Reference | Twitch Developers

Then twitch calls the eventsub notification handler with a challenge
You echo back the challenge

Then when lirik goes live, twitch will send a payload to your eventsub notification handler
You read that payload and extract the data you need.
Then you make a HTTP Post request to the Discord Webhook URL with the data from the previous step.

So

Streamer Goes Live → Twitch Eventsub webook → Your Script → Discords Webhook → Message appears in a Discord Channel

Ah so the post() is the information received via eventsub trigger you’re sending to your Discord webhook URL and you would then right in a get() into the script to display the requested information into a discord chat message.

Setting up the SSL on the server is where I’m getting very lost. At this point I’m straying from this forums objective. I’ll read more about setting up a handler.

I noticed you spend a lot of time answering peoples questions. I really do appreciate you taking the time. The world of coding is very intimidating to a beginner. Thank you

Well

Twitch → Posts to your server
Your server then Posts to the Discord webhook

It’s all posts :stuck_out_tongue:

:+1: glad to help

Oh right because the post() triggers the reply yes?

Not quite sure what you mean!

Ha me either!

I suppose I’m thinking that when you request ( ‘post()’ ?) to the discord webhook is when the discord webhook would relay the information from the payload received from the handler

When I say discord webhooks I mean

“send a message to Discord” Not “recieve a message from Discord”

Got it!

Time to go through all of this. Thank you!

Snag - so I realized a constant loop to check if a streamer is online will cause an issue of continually sending a message when they’re online (not only once).

Is there any way of handling this? Is it possible to code in a stop loop when “x” happens and then a resume loop with “x” happens?

Use eventsub

Or keep an internal list of who was live last time you checked and compare that list

Yeah eventsub is essentially the answer.

I honestly don’t know where to begin as a complete beginner. I’ll keep researching

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