Can’t validate the URL I have setup using ngrok. The error message is as follows:
Error
The URL couldn't be validated. Please try again.
Once I try validate in the app settings I get a 403 error in the terminal
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 -
My code
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)
UPDATE:
I changed if request.args.get(‘hub.verify_token’) == VERIFY_TOKEN: to if request.args.get(‘hub.verify_token’) != VERIFY_TOKEN:
This is what happens now when I try to enable webhooks integration.
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
Answers
See you have that if the token matches VERIFY TOKEN (hello), it will fail, change the condition to !=
In your code you have
return request.args['hub.challange'], 200
but in query&hub.challenge
therefore you get KeyError, as hub.challenge != hub.challange