问题描述
我正在使用 pyjwt 库来解码 JWT 令牌.我在解码时收到此错误.代码在文档中给出.
导入jwtencoding_jwt='''eyJ0eXAiOiJKV1QiLCJhbG......'''secret=b''''-----开始公钥--MIIFRjCCBC6gAwIBAgIQCIdSGhpikQCjOIY154XoqzANBgkqhkiG9w0BAQsFADBN......-----结束公钥-----'''打印(jwt.decode(encoded_jwt,秘密,算法=['RS256']))
raise ValueError("Could not deserialize key data.") ValueError: could不反序列化关键数据.
您能否帮我解决它,因为当我在 JWT 网站上使用它时,它正在工作.>
这是完整的错误日志..
回溯(最近一次调用最后一次):文件"/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/jwt/algorithms.py",第 205 行,在 prepare_key 中key = load_pem_private_key(key, password=None, backend=default_backend()) 文件"/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py",第 20 行,在 load_pem_private_key 中返回 backend.load_pem_private_key(data, password) 文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py",第 1014 行,在 load_pem_private_key 中密码,文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py",第 1233 行,在 _load_key 中self._handle_key_loading_error() 文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py",第 1291 行,在 _handle_key_loading_error 中raise ValueError("无法反序列化关键数据.") ValueError: 无法反序列化关键数据.
在处理上述异常的过程中,又发生了一个异常:
回溯(最近一次调用最后一次):文件/home/sathiyakugan/PycharmProjects/JWTsample/sample.py",第 45 行,在打印(jwt.decode(encoded_jwt,秘密,算法=['RS256']))文件"/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/jwt/api_jwt.py",第 93 行,在解码中jwt、key=key、algorithms=algorithms、options=options、**kwargs 文件"/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/jwt/api_jws.py",第 157 行,在解码中密钥,算法)文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/jwt/api_jws.py",第 221 行,在 _verify_signature 中key = alg_obj.prepare_key(key) 文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/jwt/algorithms.py",第 207 行,在 prepare_key 中key = load_pem_public_key(key, backend=default_backend()) 文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py",第 24 行,在 load_pem_public_key 中返回 backend.load_pem_public_key(data) 文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py",第 1040 行,在 load_pem_public_key 中self._handle_key_loading_error() 文件/home/sathiyakugan/PycharmProjects/Python/venv/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py",第 1291 行,在 _handle_key_loading_error 中raise ValueError("无法反序列化关键数据.") ValueError: 无法反序列化关键数据.
进程以退出代码 1 结束
pyjwt 库中存在一些问题.并且您必须从证书中获取公钥.
我使用了 openssl x509 -pubkey -noout -in cert.pem >pubkey.pem
然后从公钥我可以使用 authlib 库轻松解码它.
from authlib.specs.rfc7519 import jwtencoding_jwt='''eyJ0eXAiOiJ....'''secret=b'''-----BEGIN PUBLIC KEY-----......-----结束公钥-----'''声明 = jwt.decode(encoded_jwt, secret)打印(索赔)
I am using pyjwt library for decoding the JWT token. I got this error when I am decoding. The code was given in the documantation.
import jwt
encoded_jwt='''eyJ0eXAiOiJKV1QiLCJhbG......'''
secret=b''''-----BEGIN PUBLIC KEY-----
MIIFRjCCBC6gAwIBAgIQCIdSGhpikQCjOIY154XoqzANBgkqhkiG9w0BAQsFADBN
......
-----END PUBLIC KEY-----'''
print(jwt.decode(encoded_jwt, secret , algorithms=['RS256']))
Could You please help me in resolving it beacuse when I use this it in the JWT website it's working.
This is the full error log..
There are some issues in the pyjwt library. and you must get the public key from the certificate.
I used openssl x509 -pubkey -noout -in cert.pem > pubkey.pem
then from the public key I could easily decode it using authlib library.
from authlib.specs.rfc7519 import jwt
encoded_jwt='''eyJ0eXAiOiJ....'''
secret=b'''-----BEGIN PUBLIC KEY-----
......
-----END PUBLIC KEY-----'''
claims = jwt.decode(encoded_jwt, secret)
print(claims)
这篇关于无法反序列化解码 JWT python 的关键数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!