微信小程序登录 该死的官方文档TypeError: the JSON object must be str, not 'bytes'
官方文档代码
class WXBizDataCrypt: def __init__(self, appid, session_key): self.appid = appid self.session_key = session_key def decrypt(self, encrypted_data, iv): ''' aes decode 将加密后的信息解密 @param encrypted_data: 包括敏感数据在内的完整用户信息的加密数据 @param iv: 加密算法的初始向量 @return: 解密后数据 ''' session_key = base64.b64decode(self.session_key) encrypted_data = base64.b64decode(encrypted_data) iv = base64.b64decode(iv) cipher = AES.new(session_key, AES.MODE_CBC, iv) decrypted = json.loads(self._unpad(cipher.decrypt(encrypted_data))) if decrypted['watermark']['appid'] != self.appid: raise Exception('Invalid Buffer') return decrypted def _unpad(self, s): return s[:-ord(s[len(s)-1:])]
该死的官网文档 """ ERROR exception 135 Internal Server Error: /apple/login/ Traceback (most recent call last): File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/rest_framework/views.py", line 483, in dispatch response = self.handle_exception(exc) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/rest_framework/views.py", line 443, in handle_exception self.raise_uncaught_exception(exc) File "/home/python/.virtualenvs/py3/lib/python3.5/site-packages/rest_framework/views.py", line 480, in dispatch response = handler(request, *args, **kwargs) File "/home/python/Desktop/dongliang/dongliang/apps/wechat/views.py", line 661, in post response = applet.decrypt(encryptedData, iv) File "/home/python/Desktop/dongliang/dongliang/apps/wechat/applet_utls.py", line 45, in decrypt decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData))) File "/usr/lib/python3.5/json/__init__.py", line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not 'bytes' ERROR basehttp 124 "POST /apple/login/ HTTP/1.1" 500 22510
问题:
decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData)))
self._unpad(cipher.decrypt(encrypted_data)) 这个方法是得到一个 bytes 类型数据所以需要解码
json.loads() 这个方法需要str 是不会 bytes 否则报错
json.loads() 是json格式处理函数(可以这么理解,json是字符串)
json.loads() 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。
json.loads() 如果字符串不是字典格式的字符串的时候,执行json.loads会报错!
解决办法:
class WXBizDataCrypt: def __init__(self, appid, session_key): self.appid = appid self.session_key = session_key def decrypt(self, encrypted_data, iv): ''' aes decode 将加密后的信息解密 @param encrypted_data: 包括敏感数据在内的完整用户信息的加密数据 @param iv: 加密算法的初始向量 @return: 解密后数据 ''' session_key = base64.b64decode(self.session_key) encrypted_data = base64.b64decode(encrypted_data) iv = base64.b64decode(iv) cipher = AES.new(session_key, AES.MODE_CBC, iv) decrypted = json.loads(self._unpad(cipher.decrypt(encrypted_data)).decode()) if decrypted['watermark']['appid'] != self.appid: raise Exception('Invalid Buffer') return decrypted def _unpad(self, s): return s[:-ord(s[len(s)-1:])]