2

I want to call Amazon product advertising API from R. Given below is the quick guide of pa API 5.

https://webservices.amazon.com/paapi5/documentation/quick-start/using-curl.html

I tried to do the way it described here https://webservices.amazon.com/paapi5/documentation/sending-request.html using 'httr' but got thrown off on the signature version 4 signing process.

I tried using 'aws.signature' package for signature prior to calling the POST function, but the final output I am getting is status code 500.

Here is the code I have used

library(httr)
library(jsonlite)
library(aws.signature)
request_body=data.frame("Keywords"="Harry",
                        "Marketplace"= "www.amazon.com",
                        "PartnerTag"= "mytag-20",
                        "PartnerType"= "Associates",
                        "Access Key"="my_accesskey",
                        "Secret Key"="my_secret_key",
                        "service"="ProductAdvertisingAPIv1",                        
                        "Region"="us-east-1"
                        "Resources"="Offers.Listings.Price",
                        "SearchIndex"= "All")
request_body_json=toJSON(request_body,auto_unbox=T)
request_body_json=gsub("\\[|\\]","",request_body_json)

t=signature_v4_auth(
  datetime = format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC"),
  region = NULL,
  service="ProductAdvertisingAPIv1",
  verb="POST",
  "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems",
  query_args = list(),
  canonical_headers=c("Host: webservices.amazon.com",
                       "Content-Type: application/json; charset=UTF-8",                       
                       "X-Amz-Target: com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems",
                       "Content-Encoding: amz-1.0",
                       "User-Agent: paapi-docs-curl/1.0.0"),
  request_body=request_body_json,
  signed_body = TRUE,
  key = "access_key",
  secret = "secret-key",
  session_token = NULL,
  query = FALSE,
  algorithm = "AWS4-HMAC-SHA256",
  force_credentials = FALSE,
  verbose = getOption("verbose", FALSE)
)

result=POST("https://webservices.amazon.com/paapi5/searchitems",body=request_body_json,
            add_headers(.headers=c("Host: webservices.amazon.com",
              "Content-Type: application/json; charset=UTF-8",
                                   paste("X-Amz-Date:",format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC")),
                                   "X-Amz-Target: com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems",
                                   "Content-Encoding: amz-1.0",
                                   "User-Agent: paapi-docs-curl/1.0.0",
                                   paste0("Authorization: AWS4-HMAC-SHA256 Credential=",t[["Credential"]],"SignedHeaders=content-encoding;host;x-amz-date;x-amz-target Signature=",t[["Signature"]])
            )))

Appreciate it if anyone one can help with this. Thanks

orjay
  • 21
  • 3
  • I have the same problem, did you manage to solve it? I tried with RCurl and I couldn't access the API either. – Jorge Pradas Nov 26 '20 at 15:22
  • I couldn't. Instead I created a flask API which would call PAAPI 5 and I would call it from my shiny app. – orjay Nov 27 '20 at 16:03

0 Answers0