質問編集履歴

1

コード全文を追記

2018/01/09 04:38

投稿

pockygame
pockygame

スコア17

test CHANGED
File without changes
test CHANGED
@@ -17,6 +17,8 @@
17
17
 
18
18
 
19
19
  ========
20
+
21
+ ```Python
20
22
 
21
23
  import hmac
22
24
 
@@ -50,6 +52,8 @@
50
52
 
51
53
 
52
54
 
55
+ ```
56
+
53
57
  ========
54
58
 
55
59
 
@@ -65,3 +69,131 @@
65
69
 
66
70
 
67
71
  (参照元ブログ)https://tech.torico-corp.com/blog/amazon-marketplace-mws-api/
72
+
73
+
74
+
75
+ 【2018/01/09追記】
76
+
77
+ 署名対象の文字列を実行する部分のコードも合わせて転記させていただきます。
78
+
79
+ (実行しているコード全文です)
80
+
81
+
82
+
83
+ ```Python
84
+
85
+ import base64
86
+
87
+ import datetime
88
+
89
+ import hashlib
90
+
91
+ import hmac
92
+
93
+ from urllib import quote
94
+
95
+
96
+
97
+ import requests
98
+
99
+
100
+
101
+ AMAZON_CREDENTIAL = {
102
+
103
+ 'SELLER_ID': 'セラーID',
104
+
105
+ 'ACCESS_KEY_ID': 'AWSアクセスキーID',
106
+
107
+ 'ACCESS_SECRET': 'アクセスシークレット',
108
+
109
+ }
110
+
111
+
112
+
113
+ DOMAIN = 'mws.amazonservices.jp'
114
+
115
+ ENDPOINT = '/Orders/2013-09-01'
116
+
117
+
118
+
119
+
120
+
121
+ def datetime_encode(dt):
122
+
123
+ return dt.strftime('%Y-%m-%dT%H:%M:%SZ')
124
+
125
+
126
+
127
+ timestamp = datetime_encode(datetime.datetime.utcnow())
128
+
129
+
130
+
131
+ last_update_after = datetime_encode(
132
+
133
+ datetime.datetime.utcnow() - datetime.timedelta(days=1))
134
+
135
+
136
+
137
+ data = {
138
+
139
+ 'AWSAccessKeyId': AMAZON_CREDENTIAL['ACCESS_KEY_ID'],
140
+
141
+ 'Action': 'ListOrders',
142
+
143
+ 'MarketplaceId.Id.1': 'A1VC38T7YXB528',
144
+
145
+ 'SellerId': AMAZON_CREDENTIAL['SELLER_ID'],
146
+
147
+ 'SignatureMethod': 'HmacSHA256',
148
+
149
+ 'SignatureVersion': '2',
150
+
151
+ 'Timestamp': timestamp,
152
+
153
+ 'Version': '2013-09-01',
154
+
155
+ 'LastUpdatedAfter': last_update_after,
156
+
157
+ }
158
+
159
+
160
+
161
+ query_string = '&'.join('{}={}'.format(
162
+
163
+ n, quote(v, safe='%')) for n, v in sorted(data.items()))
164
+
165
+
166
+
167
+ canonical = "{}\n{}\n{}\n{}".format(
168
+
169
+ 'POST', DOMAIN, ENDPOINT, query_string
170
+
171
+ )
172
+
173
+
174
+
175
+ h = hmac.new(
176
+
177
+ bytes(AMAZON_CREDENTIAL['ACCESS_SECRET']),
178
+
179
+ bytes(canonical), hashlib.sha256)
180
+
181
+
182
+
183
+ signature = quote(base64.b64encode(h.digest()), safe='')
184
+
185
+
186
+
187
+ url = 'https://{}{}?{}&Signature={}'.format(
188
+
189
+ DOMAIN, ENDPOINT, query_string, signature)
190
+
191
+
192
+
193
+ response = requests.post(url)
194
+
195
+
196
+
197
+ print(response.content.decode())
198
+
199
+ ```