Ad

Python POST Request Gives Unknown Result

- 1 answer

So I'm really out of my element with RESTful stuff. I'm trying to make a thing on that subscribes to an action/webhook(?) on twitch.tv so that if someone goes live, it knows.

I want to use this webhook here:

https://dev.twitch.tv/docs/api/webhooks-reference/#topic-stream-changed

I made a Flask server in server.py:


app = Flask(__name__)

@app.route('/', methods=['POST'])
def result():
    print(request.data)
    return 'Received'

and the POST I make is over here in notify.py:

import requests
r = requests.post("https://api.twitch.tv/helix/webhooks/hub", headers={'Client-ID': client_id}, data={"hub.callback": "http://127.0.0.1:5000/","hub.mode":"subscribe",'hub.topic':"https://api.twitch.tv/helix/streams?user_id=XXXXXX"})

Running my code shows nothing on the server, not even Received so I guess I'm doing something wrong.

if I do a GET, on request = requests.get('https://api.twitch.tv/helix/streams?user_id=xxxxxx', headers={'Client-ID': client_id})

the result is b ' '

and I have no idea what that means

in notifiy.py, putting a print(r) returns <Response [202]> but I think I want a [200]

I assume my server needs to be reachable by Twitch to see it but I'm not sure.

Any help is appreciated!

Ad

Answer

Final EDIT... I have created a proof of concept


You're POSTing "hub.callback": "http://127.0.0.1:5000/". That URL is only accessible on your machine.

This is supposed to be a URL which is accessible from the Twitch infrastructure. If you can't register a domain, then you could use something like ngrok to get a valid URI which routes back to your Flask development server for testing.

Once you have sent the POST request, you can get the webhook subscriptions to confirm the post request worked. This is also possible using curl, with the commands included to the right of this documentation.

Assuming you see valid subscriptions there, then the endpoint you provide as hub.callback should receive a hit from Twitch...

when a stream changes; e.g., stream goes online or offline, the stream title changes, or the game changes.

Within the route you'd then do some logic to deal with the result of that request.


Update re comments

You may wish to try updating hub.lease_seconds: (here)

Number of seconds until the subscription expires. Default: 0. Maximum: 864000.

The default (0) allows you to test the subscription-creation workflow without creating any subscriptions (since they expire immediately). After testing, to actually create subscriptions, you must specify a larger value.

This belongs in the dictionary passed as the data argument to requests.post in notify.py:

Ad
source: stackoverflow.com
Ad