Skip to content

Could not unserialize key data (use .p8 file as private key file, use ES256 algorithm) #96

@chenhongws

Description

@chenhongws

Such was the code:
APNS_AUTH_KEY = './APNsAuthKey_SP7C6832EZ.p8'
f = open(APNS_AUTH_KEY)
secret = f.read()
token = jwt.encode(
payload={
"iss": "8X2A38Q9VD",
"iat": time.time()
},
key= secret,
algorithm= 'ES256',
headers={
"alg": "ES256",
"kid": "SP7C6832EZ",
}
)

But it run with error:
Traceback (most recent call last):
File "Http2AuthToken.py", line 31, in
"kid": "SP7C6832EZ",
File "/usr/lib/python2.7/site-packages/jwt/api_jwt.py", line 56, in encode
json_payload, key, algorithm, headers, json_encoder
File "/usr/lib/python2.7/site-packages/jwt/api_jws.py", line 98, in encode
key = alg_obj.prepare_key(key)
File "/usr/lib/python2.7/site-packages/jwt/algorithms.py", line 226, in prepare_key
key = load_pem_private_key(key, password=None, backend=default_backend())
File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
return backend.load_pem_private_key(data, password)
File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/multibackend.py", line 303, in load_pem_private_key
return b.load_pem_private_key(data, password)
File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1048, in load_pem_private_key
password,
File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1217, in _load_key
self._handle_key_loading_error()
File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1289, in _handle_key_loading_error
raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data.

Is there somebody can explain?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions