Python TypeError:需要类似字节的对象,而不是';str';-hmac

Python TypeError:需要类似字节的对象,而不是';str';-hmac,python,python-3.x,encoding,base64,hmac,Python,Python 3.x,Encoding,Base64,Hmac,我看到这个错误: TypeError: a bytes-like object is required, not 'str' python3.6/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False)` 代码如下: import base64 import hmac import hashlib import binascii .... def post(self,reque

我看到这个错误:

TypeError: a bytes-like object is required, not 'str' 
    python3.6/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False)`
代码如下:

import base64
import hmac
import hashlib
import binascii

....
def post(self,request):
    body = str(request.body).encode()
    sign_signature = base64.b64encode(hmac.new('tester'.encode(), body, hashlib.sha256).hexdigest())

将代码的该行替换为以下内容:

sign_signature = base64.b64encode(hmac.new('tester'.encode(), body, hashlib.sha256).digest())
摘要
返回
字节
->我们想对其进行B64编码,B64编码接受字节,所以我们很好