Missing or unknown grant type

Hi,

I am trying to obtain an access token via a node.js backend.

My code to do so looke like this:

I am gettin a 400 error: Missing or unknown grant type

Can you guide me in what I am doing wrong?

It works perfectly fine in curl and in Postman with the same data.

Hi @sosv

"There are some possibilities for that problem:

1. Check the redirect URL in both places (in the app and the code); the redirect URLs should be the same.
2. Ensure that the code is correct."

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!

Hi @GRajput , thank you for your response. I have double checked the redirect URI and it is the same in the app settings and in the source code (im my example it is: localhost:3000/api/hubspot/callback). The code is also correct, if I try this using cURL, then it is working fine.

Any other pointers that might help me?

Hi,

I made it work with the following code:

Looks like the qs.stringify is the trick here and changing form to data…

Hi @sosv

Please try with the updated code. I think it will help you.

const axios = require('axios');
const qs = require('qs');
let data = qs.stringify({
 'grant_type': 'authorization_code',
 'code': 'string', // Replace 'string' with the actual authorization code
 'redirect_uri': 'string', // Replace 'string' with your actual redirect URI
 'client_id': 'string', // Replace 'string' with your client ID
 'client_secret': 'string' // Replace 'string' with your client secret
});
axios.post('https://api.hubapi.com/oauth/v1/token', data, {
 headers: {
 'Content-Type': 'application/x-www-form-urlencoded'
 },
 maxBodyLength: Infinity
})
.then((response) => {
 console.log(JSON.stringify(response.data));
})
.catch((error) => {
 console.log(error);
});

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!