Python bot that send message when someone starts streaming

Hi I’m trying to make a discord bot that sends a message when someone starts streaming and I don’t know how to do it I just this and it does not work it says i have an error with OAuth and I don’t know how to add the OAuth token there. Could someone help me, please

import requests

API_ENDPOINT = “https://api.twitch.tv/helix/streams?user_login=jonasek369

Client_ID = “my_client_Id”

head = {

‘cliend_id’: Client_ID,
}

r = requests.get(url = API_ENDPOINT, headers = head)

print(r.text)

head = {

‘cliend_id’: Client_ID,
}

becomes

head = {
'client-id': Client_ID,
'authorization': 'Bearer ' + botToken
}

You also misspeltthe ClientID header

1 Like

it says that {“error”:“Unauthorized”,“status”:401,“message”:“Client ID and OAuth token do not match”}
do i need somehow to register the token ? eather wait thanks for the replay

Sounds like you probably generated an oAuth token using the twitchapps TMI token generator and you didn’t use their client-ID in the call

The advice is to use your own clientID and generate your own tokens using your own oAuth flow

1 Like

how can I make mine OAuth token? I’m working in API first time.

heh I misread your initial post and missed out the discord part…

Authentication is hjere

And you probably want to generate an App Access Token as documented here

1 Like

so if i understand this i should make request something like this

requests.request("https://id.twitch.tv/oauth2/token?client_id=uo6dggojyb8d6soh92zknwmi5ej1q2&client_secret=REMOVED&grant_type=client_credentials")

Removed your client secret.
Client secretes are essentially passwords and shouldn’t be posted to a public forum

If that performs a HTTP POST Request in python then yes

1 Like

that was the example from the documentation and thanks for the help.

i dont want to bother more but its giving me a respons : {“data”:[],“pagination”:{}}
and is there some way to just get the if hes streaming some extra code or something

the code now is:
import requests

API_ENDPOINT = “https://api.twitch.tv/helix/streams?user_login=jonasek369

Client_ID = “client_id”
botToken = “token”

head = {
‘client-id’: Client_ID,
‘authorization’: 'Bearer ’ + botToken
}

r = requests.get(url = API_ENDPOINT, headers = head, )

print(r.text)

If the user is not live then there is no stream to return.

When they are live then the API will return some data.

1 Like

oh yes thanks im an idiot it just needs a seconds thanks for all the respons.

im so sorry for bothering again but it works in the file where i test it but in the bot it just send me a
{“access_token”:“token”,“expires_in”:numbers,“token_type”:“bearer”}

@tasks.loop(seconds=10)
async def check_For_live_jonasek369(ctx):
channel = bot.get_channel(673138335614631948)
API_ENDPOINT = “https://api.twitch.tv/helix/streams?user_login=jonasek369

Client_ID = "client_id"
botToken = "bot_token"

head = {
    'client-id': Client_ID,
    'authorization': 'Bearer ' + botToken
}

r = requests.get(url=API_ENDPOINT, headers=head, )

w = r.text

if "live" in w:
    await channel.send("@everyone jonasek369 is streaming go check him out !")
    time.sleep(1800)
else:
    print(w)
    time.sleep(1)

That is the response you get from the token fetching endpoint.

I’m not sure what the problem is here

eather way thanks for all responses have a great day