i am trying to retrieve data from the amazon sp api using the httr package. what i did so far is:
(0) prerequisites
library(httr)
library(httr2)
library(jsonlite)
library(lubridate)
(1) defined data as:
data = list(
grant_type = "refresh_token",
refresh_token = "Atzr|...",
client_id = "amzn1...",
client_secret = "amzn1....")
to get (2) a 'access_token' value as POST request response from:
token_response = httr::POST(
url = "https://api.amazon.com/auth/o2/token",
body = data)
access_token = fromJSON(token_response)[["access_token"]]
i also (3) defined request params:
markentplace_endpoint = "https://sellingpartnerapi-eu.amazon.com"
marketplace_id = "A1PA6795UKMFR9"
request_params = list(
"MarketplaceId" = marketplace_id,
"CreatedAfter" = as_date(Sys.Date()-30))
and (4) and an url string:
url = paste0(markentplace_endpoint, "/orders/v0/orders")
url$query = request_params
url = paste0(paste0(markentplace_endpoint, "/orders/v0/orders"), url_build(url))
to finally (5) make a GET request
httr::GET(
url = paste0(paste0(markentplace_endpoint, "/orders/v0/orders"), url_build(url)),
add_headers(`x-amz-access-token` = access_token))
In the end, I'm already stuck at step 2, because I get the following response from my POST request:
Response [https://api.amazon.com/auth/o2/token]
Date: 2024-01-07 12:13
Status: 400
Content-Type: application/json;charset=UTF-8
Size: 392 B
i was basically following this https://www.youtube.com/watch?v=gp5kTI8I3pU tutorial and was trying to adapt to R/R Studio.
does anyone have more experience and is willing to share?
A 400 response code typically means the request you are sending is malformed. By default the httr::POST
function will encode the body as multipart/form-data
("multipart"). The amazon API specifically requires the data to be encoded with application/x-www-form-urlencoded
("form"). So to get the proper response you would use
token_response <- httr::POST(
url = "https://api.amazon.com/auth/o2/token",
encode = "form",
body = data)