Search code examples
rrestapigetebay-api

Using ebay Feed API with R


I am trying to call the ebay Feed API using R but don't understand the syntax to be used, seems like I am missing the headers in the call to the API:

> res <- GET(paste0("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216"))
> res
Response [https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216]
  Date: 2019-12-21 08:39
  Status: 400
  Content-Type: application/json
  Size: 228 B
{
  "errors" : [ {
    "errorId" : 1002,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Missing access token",
    "longMessage" : "Access token is missing in the Authorization HTTP request header."
  } 

Where should be the headers? I have seen something like this, adding parameters, would that be going on the right path? payload being a list of different authentication elements:

res_bis <- GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", authenticate() = payload, encode = "form", verbose())

Help much appreciated!

EDIT1:
I saw an information about HTTP headers:

HTTP request headers: Content-Type – Must be set to:application/x-www-form-urlencoded Authorization – The word "Basic " followed by your Base64-encoded OAuth credentials(client_id:client_secret).

I then tried the following but still got same error:

 GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", add_headers("Basic client_id:client_secret"))

EDIT2: Updating my code following Andrea's help:

> GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", 
+     add_headers(client_id = paste0("Basic", " ",your_token)), content_type("application/x-www-form-urlencoded")  )

Response [https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216]
  Date: 2019-12-21 12:17
  Status: 400
  Content-Type: application/json
  Size: 228 B
{
  "errors" : [ {
    "errorId" : 1002,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Missing access token",
    "longMessage" : "Access token is missing in the Authorization HTTP request header."
  } ]
> 

EDIT3:
Thanks to Andrea I managed to get my access token: enter image description here

But I still get the same error when I do:

your_token= "XXXXXXXXX"
GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", add_headers(client_id = paste0("Basic", " ",your_token)), content_type("application/x-www-form-urlencoded")  )

Response [https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216]
  Date: 2019-12-22 17:56
  Status: 400
  Content-Type: application/json
  Size: 228 B
{
  "errors" : [ {
    "errorId" : 1002,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Missing access token",
    "longMessage" : "Access token is missing in the Authorization HTTP request header."
  } ]

Solution

  • Based on our exchange above, the main issue was a lack of understanding of how ebay feed api auth flows. First, it is necessary to acquire an Authorization token to authenthicate future api requests.

    library(httr)
    library(jsonify)
    
    api_token <- "your_token_string"
    
    # Get authorization token
    auth_token_res <- GET("https://api.sandbox.ebay.com/identity/v1/oauth2/token",
                          add_headers(client_id = paste0("Basic", " ",api_token)),
                          content_type("application/x-www-form-urlencoded")) %>% 
      fromJSON()
    
    access_token <- auth_token_res[["access_token"]]  # parse it from JSON resp
    

    That token will be then passed in future calls with add_headers() just like before:

    # Make request
    feed_res <- GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216",
                    add_headers(Authorization = paste0("Bearer", " ",access_token)))
    
    # ... parse fields as needed from JSON  response