我创建了一个 Flask 应用程序,其端点已为 Dropbox webhook 做好了准备。Dropbox webhook 是一项服务,当我们的 Dropbox 文件夹中发生某些事件(例如上传文件)时,它会调用我们定义的 API 端点。我的应用程序的配置如下图所示,清楚地表明 webhook URI 已启用,即 Dropbox webhook 的挑战 URI 工作正常(API_KEY、API_SECRET 和 app.secret_key 隐藏在这里)。
接下来就可以看到我的flask app的代码了。问题是我希望每次将文件上传到我的 Dropbox 文件夹时都会触发 /webhook POST 调用,但它从未发生过。你知道解决这个问题的正确方法吗?谢谢你。
# App key and secret from the App console (dropbox.com/developers/apps)
APP_KEY = "XXXXXXXXXXXXX"
APP_SECRET = "YYYYYYYYYYYYY"
app = Flask(__name__)
app.debug = True
# A random secret used by Flask to encrypt session data cookies
app.secret_key = "zzzzzzzzzzzzz"
def process_user(account):
print("Yeahhhhh")
@app.route('/webhook', methods=['GET'])
def challenge():
'''Respond to the webhook challenge (GET request) by echoing back the challenge parameter.'''
resp = Response(request.args.get('challenge'))
resp.headers['Content-Type'] = 'text/plain'
resp.headers['X-Content-Type-Options'] = 'nosniff'
return resp
@app.route('/webhook', methods=['POST'])
def webhook():
'''Receive a list of changed user IDs from Dropbox and process each.'''
# Make sure this is a valid request from Dropbox
signature = request.headers.get('X-Dropbox-Signature').encode("utf-8")
if not hmac.compare_digest(signature, hmac.new(APP_SECRET, request.data, sha256).hexdigest()):
abort(403)
for account in json.loads(request.data)['list_folder']['accounts']:
threading.Thread(target=process_user, args=(account,)).start()
return ''
if __name__=='__main__':
app.run(host='0.0.0.0')
