Messenger Webhook-无法验证URL-Python

时间:2019-09-30 19:29:12

标签: python flask facebook-messenger ngrok

无法验证我使用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 -

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在您的代码中,您拥有return request.args['hub.challange'], 200,但在查询&hub.challenge中,您得到KeyError,即hub.chall e nge!= hub.chall a nge