Python POST 请求给出未知结果

Python POST request gives unknown result

所以我真的对 RESTful 的东西感到厌烦。我正在尝试在 twitch.tv 上订阅 action/webhook(?),这样如果有人上线,它就会知道。

我想在这里使用这个 webhook:

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

我在 server.py 中制作了一个 Flask 服务器:


app = Flask(__name__)

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

我做的POST在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"})

运行 我的代码在服务器上没有显示任何内容,甚至 Received 所以我想我做错了什么。

如果我执行 GET, request = requests.get('https://api.twitch.tv/helix/streams?user_id=xxxxxx', headers={'Client-ID': client_id})

结果是b ' '

我不知道那是什么意思

notifiy.py 中,放一个 print(r) returns <Response [202]> 但我想我想要一个 [200]

我假设我的服务器需要 Twitch 可以访问才能看到它,但我不确定。

感谢任何帮助!

最终编辑... 我创建了一个 proof of concept


您正在 POST"hub.callback": "http://127.0.0.1:5000/"。 URL 只能在您的计算机上访问。

这应该是一个可以从 Twitch 基础设施访问的 URL。如果您无法注册域,那么您可以使用 ngrok 之类的东西来获取有效的 URI,该 URI 将路由回您的 Flask 开发服务器以进行测试。

发送 POST 请求后,您可以 get the webhook subscriptions 确认 post 请求有效。 这也可以使用 curl,使用包含在本文档右侧的命令。

假设您在那里看到有效的订阅,那么您作为 hub.callback 提供的端点应该会收到来自 Twitch 的点击...

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

然后在路由中执行一些逻辑来处理该请求的结果。


更新评论

您不妨尝试更新 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.

这属于在 notify.py 中作为 data 参数传递给 requests.post 的字典: