質問編集履歴

1

ソースコードとエラーメッセージ

2018/03/16 06:27

投稿

Na-Sho
Na-Sho

スコア6

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,137 @@
1
1
  自分で取得について調べて実行して見たのですが、よくわからないエラーなどで、上手く行きませんでした。
2
2
 
3
3
  どなたか取得についての方法、もしくはアドバイス等を頂けると嬉しいです。
4
+
5
+
6
+
7
+ プログラムのソースコード
8
+
9
+ ```python
10
+
11
+ # requires pyjwt (https://pyjwt.readthedocs.io/en/latest/)
12
+
13
+ # pip install pyjwt
14
+
15
+
16
+
17
+
18
+
19
+ import datetime
20
+
21
+ import jwt
22
+
23
+
24
+
25
+
26
+
27
+ secret = """-----BEGIN PRIVATE KEY-----
28
+
29
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123
30
+
31
+ -----END PRIVATE KEY-----"""
32
+
33
+ keyId = "0123456789"
34
+
35
+ teamId = "9876543210"
36
+
37
+ alg = 'ES256'
38
+
39
+
40
+
41
+ time_now = datetime.datetime.now()
42
+
43
+ time_expired = datetime.datetime.now() + datetime.timedelta(hours=12)
44
+
45
+
46
+
47
+ headers = {
48
+
49
+ "alg": alg,
50
+
51
+ "kid": keyId
52
+
53
+ }
54
+
55
+
56
+
57
+ payload = {
58
+
59
+ "iss": teamId,
60
+
61
+ "exp": int(time_expired.strftime("%s")),
62
+
63
+ "iat": int(time_now.strftime("%s"))
64
+
65
+ }
66
+
67
+
68
+
69
+
70
+
71
+ if __name__ == "__main__":
72
+
73
+ """Create an auth token"""
74
+
75
+ token = jwt.encode(payload, secret, algorithm=alg, headers=headers)
76
+
77
+
78
+
79
+ print "----TOKEN----"
80
+
81
+ print token
82
+
83
+
84
+
85
+ print "----CURL----"
86
+
87
+ print "curl -v -H 'Authorization: Bearer %s' \"https://api.music.apple.com/v1/catalog/us/artists/36954\" " % (token)
88
+
89
+
90
+
91
+ ```
92
+
93
+ エラーコード
94
+
95
+ ```
96
+
97
+
98
+
99
+ Traceback (most recent call last):
100
+
101
+ File "music_token.py", line 33, in <module>
102
+
103
+ token = jwt.encode(payload, secret, algorithm=alg, headers=headers)
104
+
105
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/jwt/api_jwt.py", line 55, in encode
106
+
107
+ json_payload, key, algorithm, headers, json_encoder
108
+
109
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/jwt/api_jws.py", line 104, in encode
110
+
111
+ key = alg_obj.prepare_key(key)
112
+
113
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/jwt/algorithms.py", line 351, in prepare_key
114
+
115
+ key = load_pem_private_key(key, password=None, backend=default_backend())
116
+
117
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
118
+
119
+ return backend.load_pem_private_key(data, password)
120
+
121
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1015, in load_pem_private_key
122
+
123
+ password,
124
+
125
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1234, in _load_key
126
+
127
+ self._handle_key_loading_error()
128
+
129
+ File "/Users/shohei/anaconda2/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1292, in _handle_key_loading_error
130
+
131
+ raise ValueError("Could not deserialize key data.")
132
+
133
+ ValueError: Could not deserialize key data.
134
+
135
+
136
+
137
+ ```