Amazon Product API with R

2020-02-04 20:35发布

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."

5条回答
趁早两清
2楼-- · 2020-02-04 20:40

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

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-04 20:46

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.

查看更多
Summer. ? 凉城
4楼-- · 2020-02-04 20:49

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.

查看更多
再贱就再见
5楼-- · 2020-02-04 20:50

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 = '')
查看更多
Melony?
6楼-- · 2020-02-04 20:59

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

查看更多
登录 后发表回答