WebHook: Python3 - TypeError: Unicode-objects must be encoded before hashing

Hi,
I am using Python WebHook_Inspector.py example from devrel-examples-master provided by Asana Repo on GitHub. (i.e. devrel-examples/python/webhooks at master · Asana/devrel-examples · GitHub) I have upgraded the python code to make it work with Python 3.9.1 (like adding parenthesis after print etc.) Now when I run the webhook_inspector.py using ngrok it shows following error ( TypeError: Unicode-objects must be encoded before hashing ) Here is the stack
127.0.0.1 - - [18/Jan/2021 19:59:15] “e[35me[1mPOST /receive-webhook?project=number-removed HTTP/1.1e[0m” 500 -
Traceback (most recent call last):
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 2464, in call
return self.wsgi_app(environ, start_response)
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 2450, in wsgi_app
response = self.handle_exception(e)
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask_compat.py”, line 39, in reraise
raise value
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 2447, in wsgi_app
response = self.full_dispatch_request()
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask_compat.py”, line 39, in reraise
raise value
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 1950, in full_dispatch_request
rv = self.dispatch_request()
File “G:\Work\webhooksWin\myenv\Lib\site-packages\flask\app.py”, line 1936, in dispatch_request
return self.view_functionsrule.endpoint
File “G:\Work\webhooksWin\webhook_inspector.py”, line 153, in receive_webhook
signature = hmac.new(hook_secret. encode(‘ascii’, ‘ignore’),
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\Lib\hmac.py”, line 170, in new
return HMAC(key, msg, digestmod)
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\Lib\hmac.py”, line 93, in init
self.update(msg)
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\Lib\hmac.py”, line 113, in update
self._inner.update(msg)
TypeError: Unicode-objects must be encoded before hashing

And I think it results in ngrok throwing following error
POST /receive-webhook 500 INTERNAL SERVER ERROR

Please suggest what should be done to resolve this issue. Please note that I have tried this on windows 10 and WSL

Ok, I was able to resolve the issue by figuring out that hook_secret was coming as NULL and adding a check for it. Which is as follows

elif “X-Hook-Signature” in request.headers:
if hook_secret is None: #added for checking NULL
return “”
else:
signature = hmac.new(hook_secret.encode(‘ascii’, ‘ignore’),
msg=str(request.data), digestmod=hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature,
request.headers[“X-Hook-Signature”].encode(‘ascii’, ‘ignore’)):
app.logger.warn(“Calculated digest does not match digest from API. This event is not trusted.”)
return
contents = json.loads(request.data)
logger.info(“Received payload of %s events”, len(contents[“events”]))