微信小程序登錄 該死的官方文檔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:])]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM