额外的HTTP标头是否会影响MWS Feed API提交?

时间:2015-07-25 19:01:37

标签: delphi amazon-web-services delphi-xe indy amazon-mws

调试亚马逊MWS Feed提交是非常讨厌的,因为无论您做错了什么,Ama都会返回相同的无意义错误消息:

<Error>
  <Type>Sender</Type>
  <Code>InvalidParameterValue</Code>
  <Message>Either Action or Operation query parameter must be present.</Message>
</Error>

我100%肯定我正确构建StringToSign以及计算HMAC-SHA256等。我花了几周的时间来收集并采用一堆有用的函数来进行散列,签名,base64ing等MWS请求。用纯Pascal编写,它们都在订单和产品API上进行了测试。

现在谈到Feeds API时,我遇到了上述错误。所有参数均等于MWS Scratchpad生成的参数。我测试了由MWS Scratchpad生成的提交StringToSign,但没有运气。

到目前为止我注意到:MWS Scratchpad和我的应用程序生成的标题的数量/值之间存在差异。

Scratchpad生成以下标题(显示最少的标题):

  Host: mws.amazonservices.ca
  x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
  Content-Type: text/xml

我的应用使用Indy(在XE4中)TIdHTTP发出请求。当亚马逊返回上述错误时,Request.RawHeaders.Text包含以下内容:

  Content-Length: 251
  x-amazon-user-agent: MyApp/1.1(Language=Delphi;Platform=Windows7)
  Content-Type: text/xml
  Host: mws.amazonservices.ca
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Encoding: identity
  User-Agent: MyApp/1.1(Language=Delphi;Platform=Windows7)

默认情况下,标题会添加到Request对象中。我的第一个问题是:这些额外的标题理论上可以影响行为吗?即他们可以成为showstoppers吗?

我的第二个问题:是否有任何选项可以在Indy HTTP.Request中混合默认heades列表?为了继续进行调试,我宁愿排除额外的标头以查看请求是否有效。

更新 :(签名串)

  AWSAccessKeyId=<AWSAccessKeyId>
  &Action=GetFeedSubmissionList
  &Merchant=<MerchantId>
  &SignatureMethod=HmacSHA256
  &SignatureVersion=2
  &Timestamp=2015-07-26T09%3A04%3A59Z
  &Version=2009-01-01
  &Signature=1OI0PVgL3uh5sFXxjCzaaWEwGmW6h5e0dgLUFkPgoXg%3D

更新 :( TIdLogFile提供的完整HTTP请求/响应)

  Stat Connected.
  Sent 28.07.2015 12:28:11:
    POST / HTTP/1.1<EOL>
    Content-Type: text/xml; charset=us-ascii<EOL>
    Content-Length: 279<EOL>
    x-amazon-user-agent: MyAppNameAndVer<EOL>
    Host: mws.amazonservices.ca<EOL>
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<EOL>
    Accept-Encoding: identity<EOL>
    User-Agent: Mozilla/3.0 (compatible; Indy Library)<EOL><EOL>

  Sent 28.07.2015 12:28:11: 
    AWSAccessKeyId=<AWSAccessKeyId>
    &Action=GetFeedSubmissionList
    &MWSAuthToken=<AWSAuthToken>
    &Merchant=<MerchantId>
    &SignatureMethod=HmacSHA256
    &SignatureVersion=2
    &Timestamp=2015-07-28T10%3A28%3A09Z
    &Version=2009-01-01
    &Signature=I6euLIiVDzjZ8bbdtF840K0TJCkGh4NrUbQPtQtu78A%3D

  Recv 28.07.2015 12:28:11:
    HTTP/1.1 400 Bad Request<EOL>
    Date: Tue, 28 Jul 2015 10:28:10 GMT<EOL>
    Server: AmazonMWS<EOL>
    x-mws-request-id: 7e63280d-1db5-4d10-af40-587747d032a8<EOL>
    x-mws-timestamp: 2015-07-28T10:28:11.048Z<EOL>
    x-mws-response-context: OmAlguEmW20QT07uIdb9d25xkX+JSS7uFr1rDvXIoqXMvbUFzUMt1b5Xl2WzDaJszbwr25N/J4c=<EOL>
    Content-Type: text/xml<EOL>
    Content-Length: 324<EOL>
    Vary: User-Agent<EOL>
    nnCoection: close<EOL><EOL>
    <?xml version="1.0"?><LF><ErrorResponse xmlns="https://mws.amazonservices.com/"><LF>  <Error><LF>    <Type>Sender</Type><LF>    <Code>InvalidParameterValue</Code><LF>    <Message>Either Action or Operation query parameter must be present.</Message><LF>  </Error><LF>  <RequestID>7e63280d-1db5-4d10-af40-587747d032a8</RequestID><LF></ErrorResponse><LF>
  Stat Disconnected.

请注意:为方便您在<EOL>

之后添加CR,我格式化了回复

此处解释了混淆nnCoection标题:Cneonction and nnCoection HTTP headers

我的问题现在已经解决了。以下是我感谢不违反社区标准的人的方式; - )

Days := 1;
repeat
  IsSuccessful := DebugAmazonFeedSubmissionProc;
  Inc(Days);
until IsSuccessful or (Days = 14);

if not IsSuccessful then
begin
  AskTheGods(@StackOverflow);
  Sleep(1000); // ...wake up and drink a cup'o'tea :)
  Expert := TRemyLebeau.Create('delphi');
  try
    Expert.ReviewTheCode;
    Expert.GetTheJobDone;
  finally
    // You may dispose the instance here,
    // but the Class is one of the most valuable assets of the Community.
    // Thank you, Remy!
  end;
end;

1 个答案:

答案 0 :(得分:2)

您的请求Content-Type标头错误。您已将其设置为text/xml; charset=us-ascii,但实际上您并未实际发送XML数据。您需要根据MWS GetFeedSubmissionList文档将Content-Type(通过TIdHTTP.Request.ContentType属性)设置为application/x-www-form-urlencoded

TStrings版本TIdHTTP.Post()会为您设置ContentType,但您使用的是TStream版本,因此您必须设置ContentType手动。在这种情况下,我建议您切换到TStrings版本,让它为您处理表单字段的编码:

var
  PostData: TStringList;
begin
  PostData := TStringList.Create;
  try
    PostData.Add('AWSAccessKeyId=<AWSAccessKeyId>');
    PostData.Add('Action=GetFeedSubmissionList');
    PostData.Add('MWSAuthToken=<AWSAuthToken>');
    PostData.Add('Merchant=<MerchantId>');
    PostData.Add('SignatureMethod=HmacSHA256');
    PostData.Add('SignatureVersion=2');
    PostData.Add('Timestamp=2015-07-28T10:28:09Z'); // <-- NOT percent encoded yet!
    PostData.Add('Version=2009-01-01');
    PostData.Add('Signature=I6euLIiVDzjZ8bbdtF840K0TJCkGh4NrUbQPtQtu78A=');  // <-- NOT percent encoded yet!

    IdHTTP1.Request.CustomHeaders.Values['x-amazon-user-agent'] := 'MyAppNameAndVer';
    IdHTTP1.Post('http://mws.amazonservices.ca', PostData);
  finally
    PostData.Free;
  end;
end;