Amazon Product API with R
Asked Answered
U

5

12

I would like to use R to send requests to the Amazon Product API service.

Is there a way to authenticate and query the Amazon Product API with R without getting 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."

Unpin answered 24/11, 2011 at 2:39 Comment(0)
N
10

Try this

This should perform a search using the Product Advertising API, which I think you mean.

You need to supply the AWSAccessKeyId and AWSsecretkey,

which can be acquired on: http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/

search.amazon <- function(Keywords, SearchIndex = 'All', AWSAccessKeyId, AWSsecretkey, AssociateTag, ResponseGroup = 'Small', Operation = 'ItemSearch'){
     library(digest)
     library(RCurl)

 base.html.string <- "http://ecs.amazonaws.com/onca/xml?"
 SearchIndex <- match.arg(SearchIndex, c('All',
                                             'Apparel',
                                             'Appliances',
                                             'ArtsAndCrafts',
                                             'Automotive',
                                             'Baby',
                                             'Beauty',
                                             'Blended',
                                             'Books',
                                             'Classical',
                                             'DigitalMusic',
                                             'DVD',
                                             'Electronics',
                                             'ForeignBooks',
                                             'Garden',
                                             'GourmetFood',
                                             'Grocery',
                                             'HealthPersonalCare',
                                             'Hobbies',
                                             'HomeGarden',
                                             'HomeImprovement',
                                             'Industrial',
                                             'Jewelry',
                                             'KindleStore',
                                             'Kitchen',
                                             'Lighting',
                                             'Magazines',
                                             'Marketplace',
                                             'Miscellaneous',
                                             'MobileApps',
                                             'MP3Downloads',
                                             'Music',
                                             'MusicalInstruments',
                                             'MusicTracks',
                                             'OfficeProducts',
                                             'OutdoorLiving',
                                             'Outlet',
                                             'PCHardware',
                                             'PetSupplies',
                                             'Photo',
                                             'Shoes',
                                             'Software',
                                             'SoftwareVideoGames',
                                             'SportingGoods',
                                             'Tools',
                                             'Toys',
                                             'UnboxVideo',
                                             'VHS',
                                             'Video',
                                             'VideoGames',
                                             'Watches',
                                             'Wireless',
                                             'WirelessAccessories'))
 Operation <- match.arg(Operation, c('ItemSearch',
                                             'ItemLookup',
                                             'BrowseNodeLookup',
                                             'CartAdd',
                                             'CartClear',
                                             'CartCreate',
                                             'CartGet',
                                             'CartModify',
                                             'SimilarityLookup'))
 ResponseGroup <- match.arg(ResponseGroup, c('Accessories',
                                             'AlternateVersions',
                                             'BrowseNodeInfo',
                                             'BrowseNodes',
                                             'Cart',
                                             'CartNewReleases',
                                             'CartTopSellers',
                                             'CartSimilarities',
                                             'Collections',
                                             'EditorialReview',
                                             'Images',
                                             'ItemAttributes',
                                             'ItemIds',
                                             'Large',
                                             'Medium',
                                             'MostGifted',
                                             'MostWishedFor',
                                             'NewReleases',
                                             'OfferFull',
                                             'OfferListings',
                                             'Offers',
                                             'OfferSummary',
                                             'PromotionSummary',
                                             'RelatedItems',
                                             'Request',
                                             'Reviews',
                                             'SalesRank',
                                             'SearchBins',
                                             'Similarities',
                                             'Small',
                                             'TopSellers',
                                             'Tracks',
                                             'Variations',
                                             'VariationImages',
                                             'VariationMatrix',
                                             'VariationOffers',
                                             'VariationSummary'),
                            several.ok = TRUE)
 version.request = '2011-08-01'
 Service = 'AWSECommerceService'
 if(!is.character(AWSsecretkey)){
  message('The AWSsecretkey should be entered as a character vect, ie be qouted')
 }

 pb.txt <- Sys.time()

 pb.date <- as.POSIXct(pb.txt, tz = Sys.timezone)

 Timestamp = strtrim(format(pb.date, tz = "GMT", usetz = TRUE, "%Y-%m-%dT%H:%M:%S.000Z"), 24)

 str = paste('GET\necs.amazonaws.com\n/onca/xml\n',
        'AWSAccessKeyId=', curlEscape(AWSAccessKeyId),
             '&AssociateTag=', AssociateTag,
             '&Keywords=', curlEscape(Keywords),
             '&Operation=', curlEscape(Operation),
             '&ResponseGroup=', curlEscape(ResponseGroup),
             '&SearchIndex=', curlEscape(SearchIndex),
             '&Service=AWSECommerceService',
             '&Timestamp=', gsub('%2E','.',gsub('%2D', '-', curlEscape(Timestamp))),
             '&Version=', version.request,
             sep = '')

 ## signature test
 Signature = curlEscape(base64(hmac( enc2utf8((AWSsecretkey)), enc2utf8(str1), algo = 'sha256', serialize = FALSE,  raw = TRUE)))

 AmazonURL <- paste(base.html.string,
             'AWSAccessKeyId=', AWSAccessKeyId,
             '&AssociateTag=', AssociateTag,
             '&Keywords=', Keywords,
             '&Operation=',Operation,
             '&ResponseGroup=',ResponseGroup,
             '&SearchIndex=', SearchIndex,
             '&Service=AWSECommerceService',
             '&Timestamp=', Timestamp,
             '&Version=', version.request,
             '&Signature=', Signature
             sep = '')
 AmazonResult <- getURL(AmazonURL)
 return(AmazonResult)
}

The URL which we get from running this code wont give a signature address. To get a signature address use the following web address and paste the URL over there and click on Display Signed URL.

http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

Nanettenani answered 24/11, 2011 at 14:50 Comment(8)
I tried the following and I get this error: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing methodUnpin
It seems that the signature should be raw encoded. Signature = curlEscape(base64(hmac( enc2utf8(AWSsecretkey), enc2utf8(str), algo = 'sha256', serialize = FALSE, raw = TRUE))). Should do the trick, but I need to test this. I don't have an AWSAccessKeyId yet, so need to obtain that first.Nanettenani
Updated code with fixes, was wrong website, now using correct one, changed signing process to use raw instead of hexin characters and updated html escapes to use curlEscape. Added timestamp modifier to GMT time zone. Now the signature is the correct one.Nanettenani
Mischa, Despite the changes, I receive the same error response from Amazon. Should not version.request be quoted? In the AmazonURL assignment, should not 'Signature' be unquoted?Unpin
Good catch about the quoted signature. Tha would never work, because you supply the word signature instead of the calculated signature. FixedNanettenani
Mischa, it still doesn't work for me. Does the script correspond to the API requirements -- see here for Amazon test site: associates-amazon.s3.amazonaws.com/signed-requests/helper/…Unpin
Now it should work. I get the same signature as with the signed-request helper siteNanettenani
@mischaVreeburg -- Hi Mischa, I just stumbled upon your excellent answer and I'm trying to get the signature to match what is generated by the tester site. I'm using their example parameters in your function (along with my KeyID, ID, and Tag), but i don't get a matching sig. I also had to change the date function (replaced : with %3A) to match theirs. Any ideas on what I can do differently? Thanks so much!Alsacelorraine
O
1

See this post as well as Amazon's Signed Requests Helper. This posting, as well as the two links I've shared helped me get up and running with Amazon's Product Advertising API.

Ossieossietzky answered 3/12, 2015 at 0:51 Comment(0)
A
1

I am new and I don't have enough "rep" to comment, but in Micha's answer there needs to be a comma after Signature in this area (I have added the comma):

AmazonURL <- paste(base.html.string,
         'AWSAccessKeyId=', AWSAccessKeyId,
         '&AssociateTag=', AssociateTag,
         '&Keywords=', Keywords,
         '&Operation=',Operation,
         '&ResponseGroup=',ResponseGroup,
         '&SearchIndex=', SearchIndex,
         '&Service=AWSECommerceService',
         '&Timestamp=', Timestamp,
         '&Version=', version.request,
         '&Signature=', Signature,
         sep = '')
Aday answered 26/5, 2017 at 20:5 Comment(1)
As per the gentleman's question below, this code is for the Amazon Product Advertising API, not the Product API, the title should be changed!Aday
K
0

Check http://www.omegahat.org/ . There are several Amazon-related packages there, and even if Product API might not be among these, you should be able to copy the basic functions.

Kraigkrait answered 24/11, 2011 at 13:8 Comment(1)
Update for 2017, almost all of the omegahat Amazon packages are woefully out of date and may not work at all. If you are comfortable with Java consider cran.r-project.org/web/packages/awsjavasdk/index.html. Otherwise consider one of the cloudyr packages (github.com/cloudyr).Finkelstein
U
-1

which Amazon Product API you are interested in?

I never saw an interface for the "Product Advertising API"! For AWS you can use the package AWS tools package at CRAN: http://cran.r-project.org/web/packages/AWS.tools/index.html

Unsophisticated answered 24/11, 2011 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.