使用AWS SP-API上传Feed至Amazon时遭遇SignatureDoesNotMatch错误
AWS SP-API Feed上传SignatureDoesNotMatch错误修复
核心问题原因
- Content-Type不匹配:调用
createFeedDocument时指定的contentType是text/tab-separated-values; charset=UTF-8,但上传Feed时用的是text/xml。预签名URL的签名校验会严格匹配请求头的Content-Type,不一致直接触发签名错误。 - 请求体传递方式错误:Laravel Http客户端使用
post($url, ['body' => ...])的方式会把内容当作表单参数处理,而非直接作为请求体,导致请求格式不符合要求,进而签名校验失败。
修复步骤及代码修改
步骤1:统一Content-Type
调用createFeedDocument时,要和实际上传的Feed文件类型保持一致。如果上传XML,将contentType改为对应类型:
public function createFeedDocument($distributionChannelTeamId){ $response = Http::withHeaders([ 'x-amz-access-token' => $this->getAccessToken($distributionChannelTeamId), ])->post( 'https://sellingpartnerapi-eu.amazon.com/feeds/2021-06-30/documents', [ "contentType" => "text/xml; charset=UTF-8" // 匹配XML文件类型 ] ); Log::channel('amazon')->info('Amazon create feed document response: ', ['response' => $response->json()]); if($response->successful()){ $body = $response->json(); return $body; }else{ Log::channel('amazon')->info('Amazon create feed document failed for distribution channel team id: ' . $distributionChannelTeamId); Log::channel('amazon')->info('Amazon create feed document failed: ', ['response' => $response->json()]); } }
步骤2:正确传递请求体
使用Laravel Http的withBody方法直接传递XML内容作为请求体,避免表单参数格式问题:
public function createFeed($distributionChannelTeamId) { $res = $this->createFeedDocument($distributionChannelTeamId); $url = $res['url']; $feedDocumentId = $res['feedDocumentId']; $prefixAmazon = Storage::disk('amazon')->getDriver()->getAdapter()->getPathPrefix(); $requestXml = file_get_contents($prefixAmazon . 'createFeed.xml'); // 用withBody直接传递请求体,确保Content-Type与createFeedDocument一致 $response = Http::withHeaders([ 'Content-Type' => 'text/xml; charset=UTF-8' ])->withBody(utf8_encode($requestXml), 'text/xml')->post($url); Log::channel('amazon')->info('Amazon create feed response: ', ['response' => $response]); }
额外注意事项
- 预签名URL有效期:
createFeedDocument返回的URL仅5分钟有效,需在有效期内完成上传。 - 请求方法校验:部分场景下预签名URL要求使用PUT方法而非POST,可查看
createFeedDocument响应中是否包含method字段,若为PUT则将请求方法改为put。
内容的提问来源于stack exchange,提问作者Mica
相关产品推荐
相关产品推荐

