0

我正在使用 PyJwt 为苹果应用程序通知服务令牌身份验证生成和编码 JWT,这需要此处ES256所述的签名算法。

self.server = settings.APNS_SERVER
self.algorithm = 'ES256'
PRIVATE_KEY = settings.APNS_PRIVATE_KEY

encoded_jwt = jwt.encode(
    {'iss': settings.TEAM_ID, 'iat': time.time()},
    PRIVATE_KEY,
    algorithm=self.algorithm,
    headers={'alg': self.algorithm, 'kid': settings.APNS_KEY_ID},
)

当以这种方式通过时,我收到以下错误:

     encoded_jwt = jwt.encode(
   File "/usr/local/lib/python3.9/site-packages/jwt/api_jwt.py", line 63, in encode
     return api_jws.encode(json_payload, key, algorithm, headers, json_encoder)
   File "/usr/local/lib/python3.9/site-packages/jwt/api_jws.py", line 114, in encode
     signature = alg_obj.sign(signing_input, key)
   File "/usr/local/lib/python3.9/site-packages/jwt/algorithms.py", line 423, in sign
     der_sig = key.sign(msg, ec.ECDSA(self.hash_alg()))
 TypeError: sign() missing 1 required positional argument: 'algorithm'

RS256当我使用orHS256签名算法时,这不会发生。

为什么会这样?

4

1 回答 1

1

很可能,您的私钥没有正确生成。使用此指令生成 ES256 密钥:https ://connect2id.com/products/nimbus-jose-jwt/openssl-key-generation

于 2021-11-24T10:11:21.857 回答