"grant_type parameter is missing": Spotify API PKCE OAuth Flow Troubles

Use querystring npm package to parse the data since we're using application/x-www-form-urlencoded in the header

And change the grant_type to grant_type: "client_credentials"

var querystring = require('querystring');

const headers = {
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  }
};

let data = {
  grant_type: "client_credentials",
  code: data.code,
  redirectUri: "http://localhost:8000/callback",
  client_id: your_client_id,
  client_secret: your_client_secret,
};

we use query.stringify() for the data because the content type is application/x-www-form-urlencoded also don't use params since its a post request

axios
  .post(
    "https://accounts.spotify.com/api/token",
    querystring.stringify(data),
    headers
  )
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

I've tried every method I was able to find on the internet, nothing seemed to be working, but after a few hours, this succeeded:

const headers = {
  Authorization:
    'Basic ' +
    new Buffer(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64'),
}

const { data } = await axios.post(
  'https://accounts.spotify.com/api/token',
  'grant_type=client_credentials',
  headers: { headers },
)

this.token = data.access_token

After this, you can simply use any endpoint as seen in the Spotify API examples.