我TypeError: encoding without a string argument
在尝试验证从 Razorpay 收到的 webhook 请求时收到。
我已经使用 Flask 在 Python3.6 中编写了代码。
我知道这与request.data
我作为论点传递但无法解决问题有关。
以下是调用 webhook 时执行的代码:
@app.route('/razorpay', methods=['POST'])
def razorpay_webhook():
webhook_secret: str = MY_WEBHOOK_SECRET
signature = request.headers['X-Razorpay-Signature']
client = razorpay.Client(auth=(MY_KEY, MY_SIGNATURE))
verify = client.utility.verify_webhook_signature(request.data, signature, webhook_secret)
我得到的错误如下:
2018-12-10T13:32:16.177198+00:00 app[web.1]: [2018-12-10 13:32:16,174] ERROR in app: Exception on /razorpay [POST]
2018-12-10T13:32:16.177227+00:00 app[web.1]: Traceback (most recent call last):
2018-12-10T13:32:16.177229+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
2018-12-10T13:32:16.177231+00:00 app[web.1]: response = self.full_dispatch_request()
2018-12-10T13:32:16.177233+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-12-10T13:32:16.177235+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-12-10T13:32:16.177237+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-12-10T13:32:16.177238+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-12-10T13:32:16.177240+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
2018-12-10T13:32:16.177243+00:00 app[web.1]: raise value
2018-12-10T13:32:16.177244+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-12-10T13:32:16.177246+00:00 app[web.1]: rv = self.dispatch_request()
2018-12-10T13:32:16.177247+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
2018-12-10T13:32:16.177250+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-12-10T13:32:16.177251+00:00 app[web.1]: File "/app/app.py", line 81, in razorpay_webook
2018-12-10T13:32:16.177253+00:00 app[web.1]: verify = client.utility.verify_webhook_signature(request.data, signature, webhook_secret)
2018-12-10T13:32:16.177255+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 25, in verify_webhook_signature
2018-12-10T13:32:16.177256+00:00 app[web.1]: self.verify_signature(body, signature, secret)
2018-12-10T13:32:16.177258+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 30, in verify_signature
2018-12-10T13:32:16.177260+00:00 app[web.1]: body = bytes(body, 'utf-8')
2018-12-10T13:32:16.177269+00:00 app[web.1]: TypeError: encoding without a string argument
编辑
更改代码中的行request.data
以str(request.data)
解决上述错误。但现在我收到如下新错误:
2018-12-11T04:24:16.973496+00:00 app[web.1]: Traceback (most recent call last):
2018-12-11T04:24:16.973500+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
2018-12-11T04:24:16.973502+00:00 app[web.1]: response = self.full_dispatch_request()
2018-12-11T04:24:16.973503+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-12-11T04:24:16.973505+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-12-11T04:24:16.973507+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-12-11T04:24:16.973508+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-12-11T04:24:16.973510+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
2018-12-11T04:24:16.973512+00:00 app[web.1]: raise value
2018-12-11T04:24:16.973514+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-12-11T04:24:16.973515+00:00 app[web.1]: rv = self.dispatch_request()
2018-12-11T04:24:16.973517+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
2018-12-11T04:24:16.973519+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-12-11T04:24:16.973520+00:00 app[web.1]: File "/app/app.py", line 81, in razorpay_webook
2018-12-11T04:24:16.973522+00:00 app[web.1]: verify = client.utility.verify_webhook_signature(str(request.data), signature, webhook_secret)
2018-12-11T04:24:16.973524+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 25, in verify_webhook_signature
2018-12-11T04:24:16.973526+00:00 app[web.1]: self.verify_signature(body, signature, secret)
2018-12-11T04:24:16.973527+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 45, in verify_signature
2018-12-11T04:24:16.973529+00:00 app[web.1]: 'Razorpay Signature Verification Failed')
2018-12-11T04:24:16.973536+00:00 app[web.1]: razorpay.errors.SignatureVerificationError: Razorpay Signature Verification Failed
我已经仔细检查过,我传递的所有值都是正确的。那么可能是什么问题呢?