问题
I'm using R to make a call to the Amazon MWS API and get the following error:
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
This post helped me a lot with the Product Advertising API. However, I cannot seem to make it work on the MWS side.
Here is my code:
library(digest)
library(RCurl)
base.html.string <- "https://mws.amazonservices.com/Products/2011-10-01?"
SellerID <- 'A2UZXXXXXXXXXX'
MWSAuthToken <- 'ATVPXXXXXXXXX'
MarketplaceID <- 'ATVPXXXXXXXXX'
AWSAccessKeyId <- 'AKIAXXXXXXXXXXXXXXXX'
AWSsecretkey <- 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
ActionType <- 'GetMyPriceForASIN'
version.request = '2011-10-01'
ASINList.ASIN.1 <- 'B00XXXXXXX'
pb.txt <- Sys.time()
pb.date <- as.POSIXct(pb.txt, tz = Sys.timezone)
Timestamp = strtrim(format(pb.date, tz = "GMT", usetz = FALSE, "%Y-%m-%dT%H:%M:%SZ"), 24)
str = paste('POST\nmws.amazonservices.com\n/Products/2011-10-01\n',
'ASINList.ASIN.1=', ASINList.ASIN.1,
'&AWSAccessKeyId=', AWSAccessKeyId,
'&Action=', ActionType,
'&MWSAuthToken=', MWSAuthToken,
'&MarketplaceId=', MarketplaceID,
'&SellerId=', SellerID,
'&SignatureMethod=HmacSHA256',
'&SignatureVersion=2',
'&Timestamp=', gsub('%2E','.',gsub('%2D', '-', curlEscape(Timestamp))),
'&Version=', version.request,
sep = '')
## signature test
Signature = curlEscape(base64(hmac(enc2utf8(AWSsecretkey), enc2utf8(str), algo = 'sha256', serialize = FALSE, raw = TRUE)))
AmazonURL <- paste(base.html.string,
'ASINList.ASIN.1=', ASINList.ASIN.1,
'&AWSAccessKeyId=', AWSAccessKeyId,
'&Action=', ActionType,
'&MWSAuthToken=', MWSAuthToken,
'&MarketplaceId=', MarketplaceID,
'&SellerId=', SellerID,
'&SignatureMethod=HmacSHA256',
'&SignatureVersion=2',
'&Timestamp=', Timestamp,
'&Version=', version.request,
'&Signature=', Signature,
sep = '')
AmazonResult <- getURL(AmazonURL)
I'm using the Amazon MWS Scratchpad and made sure my string to sign matches.
My secret key does contain +'s, but I thought encoding would fix that.
Any help would be appreciated!
回答1:
I figured out my problem after reading this post. I took Amazon's examples literally and used a POST instead of GET. I also had tweaked unnecessarily with my time stamp calculation, which I fixed as well. Hope this helps someone down the road.
来源:https://stackoverflow.com/questions/36895422/using-r-to-make-amazon-mws-api-calls