MWS API签名与R不匹配

时间:2018-09-28 06:14:39

标签: r amazon-web-services http post amazon-mws

我正在尝试使用GetMatchingProductForId操作从Amazon MWS API获取数据。 当我使用Amazon MWS Scratchpad时,它工作得很好。

我现在正在尝试复制HTTP POST请求中发送的url,但收到签名错误消息。 我需要了解url请求的结构。

下面是Amazon MWS Scratchpad中请求的详细信息,我将匿名标识符设为ANONYMIZED,但这是我唯一更改的内容:

HTTP POST

POST /Products/2011-10-01?AWSAccessKeyId=ANONYMIZED
&Action=GetMatchingProductForId
&SellerId=ANONYMIZED
&SignatureVersion=2
&Timestamp=2018-09-28T05%3A45%3A43Z
&Version=2011-10-01
&Signature=ANONYMIZED
&SignatureMethod=HmacSHA256
&MarketplaceId=A13V1IB3VIYZZH
&IdType=EAN
&IdList.Id.1=9781933988665 HTTP/1.1
Host: mws.amazonservices.fr
x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
Content-Type: text/xml

要签名的字符串

POST
mws.amazonservices.fr
/Products/2011-10-01
AWSAccessKeyId=ANONYMIZED&Action=GetMatchingProductForId&IdList.Id.1=9781933988665&IdType=EAN&MarketplaceId=A13V1IB3VIYZZH&SellerId=ANONYMIZED&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2018-09-28T05%3A45%3A43Z&Version=2011-10-01

=======

现在我的问题是,(让我们想象一下我的签名是正确创建的),从HTTP POST开始,请求应该是什么样的? 这是我的尝试:

https://mws.amazonservices.fr/Products/2011-10-01?AWSAccessKeyId=ANONYMIZED&Action=GetMatchingProductForId&SellerId=ANONYMIZED&SignatureVersion=2&Timestamp=2018-09-28T05%3A52%3A33Z&Version=2011-10-01&Signature=ANONYMIZED&SignatureMethod=HmacSHA256&MarketplaceId=A13V1IB3VIYZZH&IdType=EAN&IdList.Id.1=9781933988665

但是暂存器中的'\ n'转义字符呢?最后的'HTTP/1.1'应该包括在内吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我没有MWS帐户,因此无法测试以下内容,但这是您可以使用的一种方法:

# set this to your python2 binary; you'll need to do
#   pip2 install boto
# from a command-line before using this code
Sys.setenv("RETICULATE_PYTHON"="/usr/bin/python2.7") 

library(reticulate)

boto_mws_connection <- import("boto.mws.connection")

con <- boto_mws_connection$MWSConnection(
  aws_access_key_id = ACCESS_KEY
  aws_secret_access_key = AWS_SECRET
  Merchant = MERCHANT_ID
)

con$get_matching_product_for_id(
  MarketplaceId = "A13V1IB3VIYZZH",
  IdType = "EAN",
  IdList = c("9781933988665")
)

答案 1 :(得分:0)

HTTP/1.1通常是由您的http客户端库创建的。我对R不熟悉,但是我用Google搜索,似乎有一个CURL package for R。 CURL是许多语言(包括PHP)的标准http库。我的通过curl发送XML提要的PHP代码如下:

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://mws.amazonservices.fr/Products/2011-10-01?.....your data and signature here...');
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $xmlcontent);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
    "Content-Type: text/xml",
    "Content-MD5: ".base64_encode(md5($xmlcontent,true)),
    "x-amazon-user-agent: TestScript/0.01")
);
$result = curl_exec($ch);
curl_close($ch);

通过查看this,在我看来,这应该很容易转换为CURL的R接口。