質問編集履歴

6

見出し追加と最初の動かないコードは削除

2018/06/27 09:07

投稿

setouchi
setouchi

スコア6

test CHANGED
File without changes
test CHANGED
@@ -1,126 +1,20 @@
1
- PHPでAmazonの商品を検索してみたいのですが、どうしても400エラーが帰ってきてしまいます。~~公式にもサンプルがないので~~なぜ遮断されているかわかりません。もしわかる方がいらっしゃいましたら教えてください。**問い合わせコード自体は公式のものを改造したものです。公式のページを末尾に追記。公式ではSignatureは自分で用意する感じ。Signatureの生成がおかしい。**
1
+ PHPでAmazonの商品を検索してみたいのですが、どうしても400エラーが帰ってきてしまいます。~~公式にもサンプルがないので~~なぜ遮断されているかわかりません。もしわかる方がいらっしゃいましたら教えてください。**問い合わせコード自体は公式のものを改造したものです。公式のページを末尾に追記。公式ではSignatureは自分で用意する感じ。Signatureの生成がおかしいしょうか。**
2
2
 
3
3
 
4
4
 
5
- ```php
5
+ ## 参考サイト
6
-
7
- define("AccessKeyId", "アクセスキー");
8
-
9
- define("AssociateTag", "アソシエイトタグ");
10
-
11
- define("SecretKey", "シークレットキー");
12
6
 
13
7
 
14
8
 
15
- function ItemSearch($SearchIndex, $Keywords) {
9
+ 当初[AmazonのAPIの使い方のコード自体はImplementing a Product Advertising API Request - Product Advertising API](https://docs.aws.amazon.com/AWSECommerceService/latest/GSG/ImplementinganA2SRequest.html#php-requests)を参考にし、Signatureは[OAuth1.0の署名(Signature)を作成する方法](https://syncer.jp/Web/API/OAuth/)を参考にしました。あと、誠に自分勝手で申し訳ないですが、勉強がてら今回は外部ライブラリを使わない方法でやってみたいとうのが目的でやっています。
16
10
 
17
11
 
18
12
 
19
- $Operation = "ItemSearch";
20
-
21
- $Version = "2013-08-01";
13
+ ## 実際のコード
22
-
23
- $ResponseGroup = "ItemAttributes,Offers";
24
14
 
25
15
 
26
16
 
27
- $signatureKey = rawurlencode(AccessKeyId) . "&" . rawurlencode(SecretKey);
28
-
29
-
30
-
31
- $base = "http://webservices.amazon.com/onca/xml";
32
-
33
- $baseEncode = rawurlencode($base);
34
-
35
-
36
-
37
- $params = [];
38
-
39
- foreach ([
40
-
41
- "Service" => 'AWSECommerceService',
42
-
43
- "AssociateTag" => AssociateTag,
44
-
45
- "AWSAccessKeyId" => AccessKeyId,
46
-
47
- "Operation" => $Operation,
48
-
49
- "Version" => $Version,
50
-
51
- "SearchIndex" => $SearchIndex,
52
-
53
- "Keywords" => $Keywords,
54
-
55
- "ResponseGroup" => $ResponseGroup
56
-
57
- ] as $key => $value) {
58
-
59
- $params[rawurlencode($key)] = rawurlencode($value);
60
-
61
- }
62
-
63
-
64
-
65
- ksort($params);
66
-
67
-
68
-
69
- $request_params = http_build_query($params, '', '&');
70
-
71
- $request_params = rawurlencode( $request_params );
72
-
73
- $signature_data = "GET&$baseEncode&$request_params";
74
-
75
-
76
-
77
- $hash = hash_hmac('sha1', $signature_data, $signatureKey, true);
78
-
79
- $signature = base64_encode( $hash ) ;
80
-
81
-
82
-
83
- $params2 = [];
84
-
85
- foreach ($params as $key => $value) {
86
-
87
- $params2[] = $key . "=" . $value;
88
-
89
- }
90
-
91
-
92
-
93
- $request = $base . "?" . join("&", $params2) . "&Signature=" . $signature;
94
-
95
-
96
-
97
- $response = file_get_contents($request);
98
-
99
- $parsed_xml = simplexml_load_string($response);
100
-
101
-
102
-
103
- print_r($parsed_xml);
104
-
105
- }
106
-
107
-
108
-
109
- ItemSearch('トイレシート', 'PetSupplies');
110
-
111
- ```
112
-
113
-
114
-
115
- [AmazonのAPIの使い方のコード自体はImplementing a Product Advertising API Request - Product Advertising API](https://docs.aws.amazon.com/AWSECommerceService/latest/GSG/ImplementinganA2SRequest.html#php-requests)を参考にし、Signatureは[OAuth1.0の署名(Signature)を作成する方法](https://syncer.jp/Web/API/OAuth/)を参考にしました。
116
-
117
-
118
-
119
- あと、誠に自分勝手で申し訳ないですが、勉強がてら今回は外部ライブラリを使わない方法でやってみたいとうのが目的でやっています。
120
-
121
-
122
-
123
- m6uさんに助けていただき、下記のコードまで修正し、Signatureの問題までこぎつけました。Amazon側のステータスコードは400から403へ変わりました、
17
+ その後、m6uさんに助けていただき、下記のコードまで成長し、Signatureの問題までこぎつけました。Amazon側のステータスコードは400から403へ変わりました。Signatureの生成に何らかの欠陥があり403は解決されません。
124
18
 
125
19
 
126
20
 

5

HmacSHA256に変更しSecretKeyをそのまま使えばいいのにおかしなことをしていたのを修正

2018/06/27 09:07

投稿

setouchi
setouchi

スコア6

test CHANGED
File without changes
test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
  ```php
128
128
 
129
- define("AccessKeyId", "hoge");
129
+ define("AccessKey", "hoge");
130
130
 
131
131
  define("AssociateTag", "hoge");
132
132
 
@@ -138,18 +138,6 @@
138
138
 
139
139
 
140
140
 
141
- $timeStamp = gmdate('Y-m-d\TH:i:s\Z');
142
-
143
- $Operation = "ItemSearch";
144
-
145
- $Version = "2013-08-01";
146
-
147
- $ResponseGroup = "ItemAttributes,Offers";
148
-
149
- $signatureKey = rawurlencode(AccessKeyId) . "&" . rawurlencode(SecretKey);
150
-
151
-
152
-
153
141
  $base = "http://webservices.amazon.com/onca/xml";
154
142
 
155
143
  // $base = "http://ecs.amazonaws.jp/onca/xml";
@@ -164,23 +152,27 @@
164
152
 
165
153
  "AssociateTag" => AssociateTag,
166
154
 
167
- "AWSAccessKeyId" => AccessKeyId,
155
+ "AWSAccessKeyId" => AccessKey,
168
-
156
+
169
- "Operation" => $Operation,
157
+ "Operation" => "ItemSearch",
170
-
158
+
171
- "Version" => $Version,
159
+ "Version" => "2013-08-01",
172
160
 
173
161
  "SearchIndex" => $SearchIndex,
174
162
 
175
163
  "Keywords" => $Keywords,
176
164
 
177
- "ResponseGroup" => $ResponseGroup,
165
+ "ResponseGroup" => "ItemAttributes,Offers",
166
+
178
-
167
+ "Timestamp" => gmdate('Y-m-d\TH:i:s\Z'),
168
+
179
- "Timestamp" => $timeStamp
169
+ "SignatureVersion" => 2,
170
+
171
+ "SignatureMethod" => "HmacSHA256"
180
172
 
181
173
  ] as $key => $value) {
182
174
 
183
- $params[$key] = rawurlencode($value);
175
+ $params[urlEncodeRfc3986($key)] = urlEncodeRfc3986($value);
184
176
 
185
177
  }
186
178
 
@@ -190,27 +182,31 @@
190
182
 
191
183
 
192
184
 
185
+ $parsed = parse_url($base);
186
+
193
- $signature_data = "GET " . rawurlencode($base) . " " . rawurlencode(http_build_query($params, '', '&'));
187
+ $sign = join("\n", ['GET', $parsed['host'], $parsed['path'], http_build_query($params, '', '&')]);
194
-
188
+
189
+
190
+
195
- $hash = hash_hmac('sha1', $signature_data , $signatureKey , true);
191
+ $signature = base64_encode(hash_hmac('sha256', $sign, SecretKey , true));
196
-
197
-
198
-
199
- $signature = base64_encode($hash);
192
+
200
-
201
-
202
-
193
+
194
+
203
- $params2 = [];
195
+ $queries = [];
204
196
 
205
197
  foreach ($params as $key => $value) {
206
198
 
207
- $params2[] = $key . "=" . $value;
199
+ $queries[] = $key . "=" . $value;
208
-
200
+
209
- }
201
+ }
210
-
211
-
212
-
202
+
203
+
204
+
213
- $request = $base . "?" . join("&", $params2) . "&Signature=" . $signature;
205
+ $request = $base . "?" . join("&", $queries) . "&Signature=" . urlEncodeRfc3986($signature);
206
+
207
+
208
+
209
+ var_dump($request);
214
210
 
215
211
 
216
212
 
@@ -226,6 +222,14 @@
226
222
 
227
223
 
228
224
 
225
+ function urlEncodeRfc3986($str) {
226
+
227
+ return str_replace('%7E', '~', rawurlencode($str));
228
+
229
+ }
230
+
231
+
232
+
229
233
  ItemSearch('シーツ', 'PetSupplies');
230
234
 
231
235
  ```

4

試行錯誤の上、403に変化

2018/06/27 07:23

投稿

setouchi
setouchi

スコア6

test CHANGED
File without changes
test CHANGED
@@ -117,3 +117,115 @@
117
117
 
118
118
 
119
119
  あと、誠に自分勝手で申し訳ないですが、勉強がてら今回は外部ライブラリを使わない方法でやってみたいとうのが目的でやっています。
120
+
121
+
122
+
123
+ m6uさんに助けていただき、下記のコードまで修正し、Signatureの問題までこぎつけました。Amazon側のステータスコードは400から403へ変わりました、
124
+
125
+
126
+
127
+ ```php
128
+
129
+ define("AccessKeyId", "hoge");
130
+
131
+ define("AssociateTag", "hoge");
132
+
133
+ define("SecretKey", "hoge");
134
+
135
+
136
+
137
+ function ItemSearch($SearchIndex, $Keywords) {
138
+
139
+
140
+
141
+ $timeStamp = gmdate('Y-m-d\TH:i:s\Z');
142
+
143
+ $Operation = "ItemSearch";
144
+
145
+ $Version = "2013-08-01";
146
+
147
+ $ResponseGroup = "ItemAttributes,Offers";
148
+
149
+ $signatureKey = rawurlencode(AccessKeyId) . "&" . rawurlencode(SecretKey);
150
+
151
+
152
+
153
+ $base = "http://webservices.amazon.com/onca/xml";
154
+
155
+ // $base = "http://ecs.amazonaws.jp/onca/xml";
156
+
157
+
158
+
159
+ $params = [];
160
+
161
+ foreach ([
162
+
163
+ "Service" => 'AWSECommerceService',
164
+
165
+ "AssociateTag" => AssociateTag,
166
+
167
+ "AWSAccessKeyId" => AccessKeyId,
168
+
169
+ "Operation" => $Operation,
170
+
171
+ "Version" => $Version,
172
+
173
+ "SearchIndex" => $SearchIndex,
174
+
175
+ "Keywords" => $Keywords,
176
+
177
+ "ResponseGroup" => $ResponseGroup,
178
+
179
+ "Timestamp" => $timeStamp
180
+
181
+ ] as $key => $value) {
182
+
183
+ $params[$key] = rawurlencode($value);
184
+
185
+ }
186
+
187
+
188
+
189
+ ksort($params);
190
+
191
+
192
+
193
+ $signature_data = "GET " . rawurlencode($base) . " " . rawurlencode(http_build_query($params, '', '&'));
194
+
195
+ $hash = hash_hmac('sha1', $signature_data , $signatureKey , true);
196
+
197
+
198
+
199
+ $signature = base64_encode($hash);
200
+
201
+
202
+
203
+ $params2 = [];
204
+
205
+ foreach ($params as $key => $value) {
206
+
207
+ $params2[] = $key . "=" . $value;
208
+
209
+ }
210
+
211
+
212
+
213
+ $request = $base . "?" . join("&", $params2) . "&Signature=" . $signature;
214
+
215
+
216
+
217
+ $response = file_get_contents($request);
218
+
219
+ $parsed_xml = simplexml_load_string($response);
220
+
221
+
222
+
223
+ print_r($parsed_xml);
224
+
225
+ }
226
+
227
+
228
+
229
+ ItemSearch('シーツ', 'PetSupplies');
230
+
231
+ ```

3

目的の追加

2018/06/27 05:17

投稿

setouchi
setouchi

スコア6

test CHANGED
File without changes
test CHANGED
@@ -113,3 +113,7 @@
113
113
 
114
114
 
115
115
  [AmazonのAPIの使い方のコード自体はImplementing a Product Advertising API Request - Product Advertising API](https://docs.aws.amazon.com/AWSECommerceService/latest/GSG/ImplementinganA2SRequest.html#php-requests)を参考にし、Signatureは[OAuth1.0の署名(Signature)を作成する方法](https://syncer.jp/Web/API/OAuth/)を参考にしました。
116
+
117
+
118
+
119
+ あと、誠に自分勝手で申し訳ないですが、勉強がてら今回は外部ライブラリを使わない方法でやってみたいとうのが目的でやっています。

2

上部文章の修正

2018/06/27 04:26

投稿

setouchi
setouchi

スコア6

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- PHPでAmazonの商品を検索してみたいのですが、どうしても400エラーが帰ってきてしまいます。公式にもサンプルがないのでなぜ遮断されているかわかりません。もしわかる方がいらっしゃいましたら教えてください。
1
+ PHPでAmazonの商品を検索してみたいのですが、どうしても400エラーが帰ってきてしまいます。~~公式にもサンプルがないので~~なぜ遮断されているかわかりません。もしわかる方がいらっしゃいましたら教えてください。**問い合わせコード自体は公式のものを改造したものです。公式のページを末尾に追記。公式ではSignatureは自分で用意する感じ。Signatureの生成がおかしいんですかね。**
2
2
 
3
3
 
4
4
 

1

参考サイトの追加

2018/06/27 04:14

投稿

setouchi
setouchi

スコア6

test CHANGED
File without changes
test CHANGED
@@ -109,3 +109,7 @@
109
109
  ItemSearch('トイレシート', 'PetSupplies');
110
110
 
111
111
  ```
112
+
113
+
114
+
115
+ [AmazonのAPIの使い方のコード自体はImplementing a Product Advertising API Request - Product Advertising API](https://docs.aws.amazon.com/AWSECommerceService/latest/GSG/ImplementinganA2SRequest.html#php-requests)を参考にし、Signatureは[OAuth1.0の署名(Signature)を作成する方法](https://syncer.jp/Web/API/OAuth/)を参考にしました。