11

我想使用 R 向亚马逊产品 API 服务发送请求。

有没有办法使用 R 验证和查询 Amazon 产品 API 而不会出现以下错误:

“我们计算的请求签名与您提供的签名不匹配。请检查您的 AWS 秘密访问密钥和签名方法。有关详细信息,请参阅服务文档。”

4

5 回答 5

10

试试这个

这应该使用产品广告 API 执行搜索,我认为您的意思是。

您需要提供 AWSAccessKeyId 和 AWSsecretkey,

可在以下网址获得: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)
}

我们通过运行此代码获得的 URL 不会给出签名地址。要获取签名地址,请使用以下网址并将 URL 粘贴到那里,然后单击“显示签名 URL”。

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

于 2011-11-24T14:50:06.010 回答
1

请参阅此帖子以及 Amazon 的Signed Requests Helper。这篇文章以及我分享的两个链接帮助我开始使用亚马逊的产品广告 API。

于 2015-12-03T00:51:23.133 回答
1

我是新来的,我没有足够的“代表”来发表评论,但在 Micha 的回答中,该区域的签名后需要一个逗号(我已经添加了逗号):

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 = '')
于 2017-05-26T20:05:40.490 回答
0

检查 http://www.omegahat.org/。那里有几个与亚马逊相关的包,即使产品 API 可能不在其中,您也应该能够复制基本功能。

于 2011-11-24T13:08:16.717 回答
-1

您对哪个亚马逊产品 API 感兴趣?

我从未见过“产品广告 API”的接口!对于 AWS,您可以在 CRAN 使用包 AWS 工具包:http: //cran.r-project.org/web/packages/AWS.tools/index.html

于 2011-11-24T14:02:35.593 回答