Refreshing the oAuth token

I’m trying to refresh my oAuth token like that

const options = {
    method: "POST",
    headers: {
      "Authorization": `Basic ${authorization}`,
      "Content-Type": "application/x-www-form-urlencoded",
    },
    body: {
      "grant_type": "refresh_token",
      "refresh_token": UserPipeDrive?.refresh_token /* as string */,
    },
  };

  const newToken = await fetch(
    "https://oauth.pipedrive.com/oauth/token",
    options
  )
    .then((response) => response.json())
    .then((response) => {
      return response;
    })
    .catch((err) => console.error(err));

But ! the PipeDrive API send me an error about the parameter grant_type, this is the error :

ERROR:  {
   success: false,
   message: 'Missing parameter: `grant_type`',
   error: 'invalid_request'
}

the parameter grant_type is send in the body like the doc showed.

Hi @WebMaster,

Welcome to Pipedrive’s Developer Community!

Since you’re using native node fetch for x-www-form-urlencoded request, you need to transform body with stringify function from querystring module:

const qs = require('querystring');

const options = {
    ...
    body: qs.stringify({
        "grant_type": "refresh_token",
        "refresh_token": UserPipeDrive?.refresh_token /* as string */,
    })
};

or, as alternative, provide body as instance of URLSearchParams:

const options = {
    ...
    body: new URLSearchParams({
        "grant_type": "refresh_token",
        "refresh_token": UserPipeDrive?.refresh_token /* as string */,
    }),
};

Both solutions will make a request with correct payload.