How to get access_token using refresh token from Ebay?
Asked Answered
R

1

6

I'm trying to get access_token from a refresh token I got from eBay. I'm using Axios but I keep getting getting grant type in request is not supported error.

  const refreshToken = 'xxxxxxxx';
  const appID = 'xxxxxxx';
  const certID = 'xxxxxx';
  const scopes = [
        'https://api.ebay.com/oauth/api_scope',
        'https://api.ebay.com/oauth/api_scope/sell.fulfillment',
        'https://api.ebay.com/oauth/api_scope/sell.account',
        'https://api.ebay.com/oauth/api_scope/sell.inventory'
  ]
  const params = {
    grant_type: 'refresh_token',
    refresh_token: refreshToken,
    'scope': encodeURI(scopes.join(" ")),
    // scope: scopes,
  

const token = Buffer.from(`${appID}:${certID}`);
const URL = 'https://api.ebay.com/identity/v1/oauth2/token'
const { data } = await axios.post(URL, params, {
      'headers': {
        'Authorization': `Basic ${token.toString('base64')}`,
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    })
Redeploy answered 22/3, 2021 at 11:12 Comment(0)
R
5

Turns out you'll have to UriEncode the entire request body, made use of node's querystring.encode():

const qs = require("querystring")
...
const { data } = await axios.post(URL, qs.encode(params), {
  'headers': {
    'Authorization': `Basic ${token.toString('base64')}`,
    'Content-Type': 'application/x-www-form-urlencoded',
  },
})
Redeploy answered 23/3, 2021 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.