问题描述
我想使用 R 向亚马逊产品 API 服务发送请求.
I would like to use R to send requests to the Amazon Product API service.
有没有一种方法可以使用 R 验证和查询 Amazon Product API 而不会出现以下错误:
Is there a way to authenticate and query the Amazon Product API with R without getting the following error:
我们计算的请求签名与您提供的签名不匹配.请检查您的 AWS 秘密访问密钥和签名方法.有关详细信息,请参阅服务文档."
"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."
推荐答案
试试这个
这应该使用 Product Advertising API 执行搜索,我认为您的意思是.
This should perform a search using the Product Advertising API, which I think you mean.
您需要提供 AWSAccessKeyId 和 AWSsecretkey,
You need to supply the AWSAccessKeyId and 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)
}
这篇关于亚马逊产品 API 与 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!