0

I am trying to generate Apple Music JWT token.

I have valid keyId, teamId and .p8 file + I have installed latest version of pyjwt & cryptography.

When I am trying to execute python music_token.py I am receiving following error.

(Downloaded music_token.py script for here)

Traceback (most recent call last):
  File "music_token.py", line 33, in <module>
    token = jwt.encode(payload, secret, algorithm=alg, headers=headers)
  File "/Library/Python/2.7/site-packages/jwt/api_jwt.py", line 56, in encode
    json_payload, key, algorithm, headers, json_encoder
  File "/Library/Python/2.7/site-packages/jwt/api_jws.py", line 102, in encode
    key = alg_obj.prepare_key(key)
  File "/Library/Python/2.7/site-packages/jwt/algorithms.py", line 351, in prepare_key
    key = load_pem_private_key(key, password=None, backend=default_backend())
  File "/Library/Python/2.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "/Library/Python/2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1015, in load_pem_private_key
    password,
  File "/Library/Python/2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1234, in _load_key
    self._handle_key_loading_error()
  File "/Library/Python/2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1292, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.

Can someone please point me in correct direction If I am doing something wrong here?

I tried searching on google and it hints that there could be some problem with installed version jwt but I have installed latest version.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Vipul
  • 27,808
  • 7
  • 60
  • 75

1 Answers1

1

I got the same error when copy paste the private key to music_token.py. But it worked when I did this instead of secret = "".

APNS_AUTH_KEY = './APNsAuthKey_SP7C6832EZ.p8'
f = open(APNS_AUTH_KEY)
secret = f.read()
Fazal
  • 11
  • 1