用于 Dropbox webhook 的 Flask 应用程序,通知不起作用

Flask app for Dropbox webhook, notifications not working

我创建了一个 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')

如果您没有收到来自 Dropbox 的预期 webhook notification 请求,需要检查一些事项。确保你有:

  • 正确的应用程序:如果您可能注册了多个应用程序,请确保将 webhook URI 添加到正确的应用程序
  • 正确的网络钩子 URI:确保您在网络书 URI 中注册了正确的 host/port/path。 (The dropbox_hook project 可用于轻松模拟 webhook 通知请求。)
  • 在正确的帐户中进行更改:确保您在正确的帐户中进行更改。 Dropbox webhook 通知只会针对当前连接到 API 应用程序(例如,由 the OAuth app authorization flow, or by generating an access token on the App Console 授权)的帐户中的更改发送。
  • 更改正确的文件夹,如果使用 "app folder" 权限:对于使用 "app folder" permission 的应用,例如您的应用,只会针对在已连接用户的帐户中创建的特殊应用程序文件夹(对于使用英语的帐户,默认情况下位于 /Apps/APP_FOLDER_NAME),而不是帐户中的其他任何位置。