skip to Main Content

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?

2

Answers


  1. I am leaving here a working example. It is very difficult to do this but i did.
    First i am getting app access token then i am using it to a search api. It is node.js code.

    var axios = require('axios');
    var qs = require("querystring");
    //The client credentials grant flow
    //https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html
    
    axios("https://api.ebay.com/identity/v1/oauth2/token", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Basic " + Buffer.from(
                //App ID (Client ID):
                `your id and secret key here with : seperated`
            ).toString('base64')
        },
        data: qs.stringify({
            grant_type: "client_credentials",
            scope: "https://api.ebay.com/oauth/api_scope",
            // parsed from redirect URI after returning from eBay,
    
        })
    })
        .then(response => {
            console.log("Application data");
            console.log(response.data.access_token);
    
            axios("https://api.ebay.com/buy/browse/v1/item_summary/search", {
                method: "GET",
                headers: {
                    Authorization: "Bearer " + response.data.access_token
                },
                params: {
                    category_ids: "108765",
                    q: "Beatles"
                }
            })
                .then(res => {
                    // console.log(res.data);
                    res.data.itemSummaries.map((e) => console.log(e.title));
                })
                .catch(err => {
                    console.log("**************Get search error**************")
                    console.log(err)
                });
        })
        .catch(err => {
            console.log("**************Get access token error**************")
            console.log(err)
        });
    
    Login or Signup to reply.
  2. 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"]]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search