簡體   English   中英

帶有R的Amazon Product API

[英]Amazon Product API with R

我想使用R將請求發送到Amazon Product API服務。

有沒有辦法使用R對Amazon Product API進行身份驗證和查詢,而不會收到以下錯誤:

“我們計算的請求簽名與您提供的簽名不匹配。請檢查您的AWS秘密訪問密鑰和簽名方法。有關詳細信息,請參閱服務文檔。”

試試這個

這應該使用產品廣告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不會給出簽名地址。 要獲取簽名地址,請使用以下Web地址並將URL粘貼到該處,然后單擊“顯示簽名URL”。

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

請參閱此帖以及亞馬遜的簽名請求幫助程序 這篇帖子以及我分享的兩個鏈接幫助我啟動並運行了亞馬遜的產品廣告API。

我是新手,我沒有足夠的“代表”發表評論,但在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 = '')

查看http://www.omegahat.org/ 那里有幾個與Amazon相關的包,即使Product API可能不在其中,您也應該能夠復制基本功能。

您感興趣的Amazon Product API?

我從未見過“產品廣告API”的界面! 對於AWS,您可以使用CRAN上的AWS工具包: http//cran.r-project.org/web/packages/AWS.tools/index.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM