1

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.datastr(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

我已经仔细检查过,我传递的所有值都是正确的。那么可能是什么问题呢?

4

3 回答 3

0

将 webhook 请求正文转换为字符串并删除所有尾随空格并修复缩进级别会有所帮助。

import razorpay
client = razorpay.Client(auth=("<YOUR_KEY_ID>", "<YOUR_KEY_SECRET>"))
client.utility.verify_webhook_signature(json.dumps(body, separators=(',', ':')), signature, secret)
于 2019-09-11T03:06:30.887 回答
0

stringify json 请求数据后,它在 django 中工作,试试这个,它将工作 100%

payload_body = json.dumps(request.data, separators=(',', ':'))
    verify = client.utility.verify_webhook_signature(payload_body, signature, webhook_secret)

如果在烧瓶中遇到问题,请使用

payload_body = json.dumps(request.json, separators=(',', ':'))
于 2019-04-09T06:38:40.940 回答
0

使用 Java 的 Razorpay webhook 签名验证(发布可能对其他在类似集成中苦苦挣扎的人有帮助)

@POST
@Path("/event")
public Response webhookEvent(@Context HttpServletRequest request) throws IOException,RazorpayException {
    String signature = request.getHeader("X-Razorpay-Signature");
    String body = org.apache.commons.io.IOUtils.toString(new BufferedReader(new InputStreamReader(request.getInputStream())));
    if(!isValidRequest(body, signature, "your secret")) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    JSONObject event = new JSONObject(body);
    // do stuff with the event
    return Response.status(Response.Status.OK).build();
}

private boolean isValidRequest(String body, String signature, String secret) throws RazorpayException {
    if(!Utils.verifyWebhookSignature(body, signature, secret)) {
        log.error("Invalid signature received in the request {}", signature);
        return false;
    }
    return true;
}
于 2021-05-25T16:43:47.317 回答