Python POST请求提供未知结果

2024-05-19 23:03:21 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我真的不喜欢安静的东西。我想在订阅action/webhook(?)在抽搐.tv所以如果有人活着,它就知道了。你知道吗

我想在这里使用这个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'

我发的帖子在这里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,所以我猜我做错了什么。你知道吗

如果我做了一件事 request = requests.get('https://api.twitch.tv/helix/streams?user_id=xxxxxx', headers={'Client-ID': client_id})

结果是b ' '

我不知道那是什么意思

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

我假设我的服务器需要通过Twitch访问才能看到它,但我不确定。你知道吗

感谢您的帮助!你知道吗


Tags: pyhttps服务器apiidappflasktopic
1条回答
网友
1楼 · 发布于 2024-05-19 23:03:21

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


您正在发布"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.

它属于作为data参数传递给notify.pyrequests.post的字典:

相关问题 更多 >