Search code examples
rpostoauth-2.0ebay-apihttr

R: eBay OAuth - Client Credentials Grant Request


I'm trying to get my head around OAuth, and decided to play around with the eBay API. While following their instructions to get an Application access token, I'm getting a 400 error.

library(jsonlite)
library(httr)

# OAuth credentials
client_id <- "x"
client_secret <- "x"
# Required - https://developer.ebay.com/api-docs/static/oauth-base64-credentials.html
encod_oauth <- base64_enc(paste0(client_id, ":", client_secret))

auth_token_res <- 
  POST("https://api.sandbox.ebay.com/identity/v1/oauth2/token",
       add_headers("Content-Type" = "application/x-www-form-urlencoded",
                   Authorization = paste0("Basic ", encod_oauth)),
       body = list(grant_type = "client_credentials", 
                   scope = urlEncode("https://api.ebay.com/oauth/api_scope", reserved = T)))

Examining the content, this is apparently to do with the grant_type in the body.

content(auth_token_res)
$error_description
[1] "grant type in request is not supported by the authorization server"

What is wrong with the body request and why?


Solution

  • it took one and a half hour but i did. you can find code below, written with R. Have nice day. I hope this will be the solution.

    library(httr)
    library(jsonlite)
    library(base64enc)
    
    rm(list=ls()) # delete memory
    code <- base64encode(charToRaw("TT"));
    
    #getting data from api 
    resp <- POST("https://api.ebay.com/identity/v1/oauth2/token",
                 query = list(grant_type="client_credentials",scope="https://api.ebay.com/oauth/api_scope"),
                add_headers(`Content-Type`='application/x-www-form-urlencoded',
                            `Authorization`=paste("Basic",code, sep=" ")))
    
    if (http_type(resp) != "application/json") {
      stop("API did not return json", call. = FALSE)
    }
    parsed <- jsonlite::fromJSON(content(resp, "text",encoding = "UTF-8"), simplifyVector = TRUE)
    parsed_v2 <- parsed[["response"]]