Messenger webhook - 无法验证 URL - Python

Messenger webhook - can't verify the URL - Python

无法验证我使用 ngrok 设置的 URL。报错信息如下:

Error
The URL couldn't be validated. Please try again.

一旦我尝试在应用程序设置中进行验证,我就会在终端中收到 403 错误

127.0.0.1 - - [30/Sep/2019 20:26:39] "GET /webhook?hub.mode=subscribe&hub.challenge=607343413&hub.verify_token=hello HTTP/1.1" 403 -

我的代码

import os, sys
from flask import Flask,request
app = Flask(__name__)

VERIFY_TOKEN = "hello"

@app.route('/', methods=['GET'])
def index():
    return "This is a Facebook Messenger test bot server."


@app.route('/webhook', methods=['GET'])
def verify():
    # webhook verification
    if request.args.get('hub.mode') == 'subscribe' and request.args.get('hub.challenge'):
        if request.args.get('hub.verify_token') == VERIFY_TOKEN:
            return 'Verification token missmatch', 403
        return request.args['hub.challange'], 200
    return 'verification token', 200

if __name__ == '__main__':
    app.run(debug = False)  

更新:

我将 if request.args.get('hub.verify_token') == VERIFY_TOKEN: 更改为 if request.args.get('hub.verify_token') != VERIFY_TOKEN:

这就是我尝试启用 webhooks 集成时发生的情况。

127.0.0.1 - - [30/Sep/2019 20:45:49] "GET /webhook?hub.mode=subscribe&hub.challenge=2128593822&hub.verify_token=hello HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/users/ec4/user/super-duper-telegram/app.py", line 18, in verify
    return request.args['hub.challange'], 200
  File "/users/ec4/user/super-duper-telegram/env/lib/python3.6/site-packages/werkzeug/datastructures.py", line 443, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'hub.challange'
127.0.0.1 - - [30/Sep/2019 20:45:57] "GET /webhook HTTP/1.1" 200 -

看到你有,如果令牌匹配 VERIFY TOKEN (hello),它将失败,将条件更改为 !=

在你的代码中你有 return request.args['hub.challange'], 200 但在查询中 &hub.challenge 因此你得到 KeyError,如 hub.challenge != hub.challange