简单的 WebHooks 接收器我正在关注本教程关于使用 kloudless 的 webhook。我下载了 ngrok 并设置了一个指向本地服务器的 URL,然后使用Python -m SimpleHTTPServer 8080
. 我使用 ngrok 给我的 URL 运行 POST 请求,并且能够查看/导航我的本地目录。我想使用这个脚本来验证 webhook 发件人并启动一个任务。
#!/usr/bin/env python
from flask import Flask, request, abort
import hmac
import base64
import hashlib
import threading
app = Flask('demo_app')
APP_ID = ''
API_KEY = ''
@app_route('/webook', methods=['POST']) //FIX. @app_route should be @app.route
def receiver():
# Verify the Request Signature
digest = hmac.new(API_KEY,msg=request.data,digestmod=hashlib.sha256).digest()
sig = base64.b64encode(digest)
if not sig == request.headers.get('X-Kloudless-Signature'):
abort(403)
# Since the request is verified we can actually do something
account_id = request.form.get('account')
if account_id:
threading.Thread(target=process_account, args=(account_id,)).start()
# In order to verify that the endpoint is alive you should always return your application id with a 200 OK response
return APP_ID
if __name__ == 'main':
app.run()
我收到一个错误
@app_route('/webhook ', methods=['POST'])
NameError: name 'app_route' is not defined
我是否指定了 ngrok 使用 localhost 设置的 URL @app_route
?
更新
@app_route
应该留下作为外部/webhook
URL 应该https://046f3f46.ngrok.io/webhook
作为示例。现在我试图弄清楚为什么在我在当前 webhook 下将这个外部 URL 添加到我的开发门户之后,POST
当应用程序明显支持它时,它给了我一个不受支持的方法。
更新
所以 PythonSimpleHTTPSever
不支持 POST 方法。我发现这个python 服务器确实支持 GET 和 POST 方法(它应该支持),但是我得到一个 405 错误代码,这又是一个不受支持的方法。不知道发生了什么,它必须是环境中的某些东西。