Failed to get stream data from Twitch API

Hi, I am trying to make a webhook integration to discord when there is a warframe drop enabled stream goes live, i want to be notified. However, this code is not working properly can you help me to fix it?

The problem is “Failed to get stream data from Twitch API” I receive this error.

# Import necessary libraries
import requests
import time

# Set up the Twitch API client
client_id = "********I cannot find this**********"
api_url = "https://api.twitch.tv/helix/streams"

# Set up the Discord API client
discord_api_url = "https://discord.com/api/webhooks/*******************************************"

# Set up the parameters for the Twitch API request
params = {
    "game_id": "33214",  # Warframe game ID
    "type": "live",  # Only show live streams
    "first": 100,  # Maximum number of results to return
    "language": "en"  # Only show English streams
}

while True:  # Loop indefinitely
    # Make the Twitch API request
    response = requests.get(api_url, params=params, headers={"Client-ID": client_id})

    # Check the response status code
    if response.status_code == 200:
        # If the request is successful, get the list of streams
        streams = response.json()["data"]

        # Loop through the list of streams
        for stream in streams:
            # Check if the stream has Warframe drops enabled
            if "warframe_drops_enabled" in stream and stream["warframe_drops_enabled"]:
                # Get the streamer's name and the stream's title
                streamer_name = stream["user_name"]
                stream_title = stream["title"]

                # Set up the payload for the Discord API request
                payload = {
                    "username": "Warframe Stream Alert",
                    "content": f"{streamer_name} is streaming Warframe with drops enabled!\n\n{stream_title}"
                }

                # Make the Discord API request
                requests.post(discord_api_url, json=payload)

                print(f"Notified Discord of stream by {streamer_name} with drops enabled.")
    else:
        print("Failed to get stream data from Twitch API")

    # Wait for 2 minutes before checking again
    time.sleep(120)

Theres no bearer authorization token here

Nor any code to generate a token

Client ID’s are obtained from the Developer Console after signing up and creating a ClientID

Dev Console: Twitch Developers
Getting Started Guide: Get Started | Twitch Developers

This looks like python.

This python example that does code generation and and streams call might help you out

how can i fill this according to streams for game category: Warframe. I do not know that much coding. Can you show me an example.

And, i did not understand what to write here:
Whats OAuth Redirect URLs, can i use localhost link?

For your application you can leave it as http://localhost as you are not using user oAuth.

Your original code seems correct it was just missing the token generation and usage there of

I have edited like this but there is another problem. What is the wrong statement here?

while True:  # Loop indefinitely
    # Make the Twitch API request
    response = requests.get(https://api.twitch.tv/helix/streams, params=&game_id=33214, headers={"p1fynh1uq4**********5lge5erl": client_id})

Error:

SyntaxError: invalid syntax

Can you help me?

You don’t need to post/delete and specifically reply to me when theres only us two here anyway. :stuck_out_tongue:

You put the headers back to front.

You did {value: key} instead of {key: value}

As per the example I linked

Then it should be

    response = requests.get('https://api.twitch.tv/helix/streams?game_id=33214', headers={'Client-ID': yourClientID, 'Authorization': 'Bearer AccessToken'})

Please note: I’m not a python native so this could be incorrect

I have changed the code a bit but now it stucks :smiley:

Code:

# Import necessary libraries
import requests
import time

client_id = 'p1fynh**************f35lge5erl'
client_secret = 'fh1zym**********3rirtgj8yc'

body = {
    'client_id': client_id,
    'client_secret': client_secret,
    "grant_type": 'client_credentials'
}
r = requests.post('https://id.twitch.tv/oauth2/token', body)

#data output
keys = r.json()

print(keys)

headers = {
    'Client-ID': client_id,
    'Authorization': 'Bearer ' + keys['access_token']
}

print(headers)

discord_api_url = "https://discord.com/api/webhooks/105309019591************************************sSxBMBGvRgCCEISC9K0"

# Set up an infinite loop to check for streams with Warframe drops enabled
while True:
    # Make a request to the Twitch API to get a list of live streams
    response = requests.get(
        "https://api.twitch.tv/helix/streams",
        headers=headers
    )

    params = {
        "game_id": "33214",  # Warframe game ID
        "type": "live",  # Only show live streams
        "first": 100,  # Maximum number of results to return
        "language": "en"  # Only show English streams
}
    # Check the response status code
    if response.status_code == 200:
        # If the request is successful, get the list of streams
        streams = response.json()["data"]

        # Loop through the list of streams
        for stream in streams:
            # Check if the stream has Warframe drops enabled
            if "warframe_drops_enabled" in stream and stream["warframe_drops_enabled"]:
                # Get the streamer's name and the stream's title
                streamer_name = stream["user_name"]
                stream_title = stream["title"]

                # Set up the payload for the Discord API request
                payload = {
                    "username": "Warframe Stream Alert",
                    "content": f"{streamer_name} is streaming Warframe with drops enabled!\n\n{stream_title}"
                }

                # Make the Discord API request
                requests.post(discord_api_url, json=payload)

                print(f"Notified Discord of stream by {streamer_name} with drops enabled.")
    else:
        print("Failed to get stream data from Twitch API")

    # Wait for 2 minutes before checking again
    time.sleep(120)

CMD:

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