401 HTTP Basic: Access denied. when trying to request access token

Hi all,

I have a nodejs project I am working on for us to be able to integrate FreeAgent into various other systems we use. I am following the documentation and have got the Authorisation request working so I have the authorisation code.

Straight after getting the authorisation code I send a request from the client app to our internal api server with the auth code which then calls the token_endpoint but I am getting a 401 Access denied response and I can’t figure out why.

This is the code I am using to call the FreeAgent api:

try {

  let clientId = 'OAuth identifier from dashboard';
  let clientSecret = 'OAuth secret from dashboard';
  let base64 = btoa(`${clientId}:${clientSecret}`);
  let response = await axios.post(`https://api.freeagent.com/v2/token_endpoint`, {
    grant_type: 'authorization_code',
    code: authCode,
    redirect_uri: 'http%3A%2F%2Flocalhost%3A3001%2Ffreeagent%2Fconnected'
  }, {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Basic ${base64}`
    }
  });

  console.log(response);

} catch (error) {

  console.log(error);

}

Any help would be really appreciated! Thank you!