0

我正在尝试生成 Apple Music JWT 令牌。

我有有效的 keyId、teamId 和 .p8 文件 + 我已经安装了最新版本的 pyjwt & cryptography。

当我尝试执行时,python music_token.py我收到以下错误。

(在这里下载了 music_token.py 脚本)

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.

如果我在这里做错了什么,有人可以指出我正确的方向吗?

我尝试在谷歌上搜索,它提示安装的版本可能存在问题,jwt但我安装了最新版本。

4

1 回答 1

1

将私钥复制粘贴到 music_token.py 时出现同样的错误。但是当我这样做而不是秘密=“”时它起作用了。

APNS_AUTH_KEY = './APNsAuthKey_SP7C6832EZ.p8'
f = open(APNS_AUTH_KEY)
secret = f.read()
于 2017-12-20T19:52:09.723 回答