Server to server oauth "unsupported grant type"

Hi,

I’m trying to update our node.js app to use Server to server oauth. I have enabled Server-to-server OAuth app in role management. All the settings in in the Server to server oauth app seeem to be ok. The app is published.

This is the error I get: Error: Failed to generate access token: {“reason”:“unsupported grant type”,“error”:“unsupported_grant_type”}

Is the grant type supposed to be “account_credentials”? Can you please review my code. I have double checked that clientid,secret and accountid are correct.

Here is my code:

 private generateOauth(): Promise<string> {
   const clientID: string = "xxx";
   const clientSecret: string = "xxx";
   const accountID = "xxx";
 
   const authHeader = Buffer.from(`${clientID}:${clientSecret}`).toString('base64');
 
   const data = JSON.stringify({
     grant_type: 'account_credentials',
     account_id: accountID,
   });
 
   const options = {
     hostname: 'zoom.us',
     path: '/oauth/token',
     method: 'POST',
     headers: {
       'Content-Type': 'application/json',
       'Authorization': `Basic ${authHeader}`,
       'Content-Length': data.length,
     },
   };
 
   return new Promise((resolve, reject) => {
     const req = https.request(options, res => {
       let responseData = '';
 
       res.on('data', chunk => {
         responseData += chunk;
       });
 
       res.on('end', () => {
         if (res.statusCode === 200) {
           const response = JSON.parse(responseData);
           resolve(response.access_token);
         } else {
           reject(new Error(`Failed to generate access token: ${responseData}`));
         }
       });
     });
 
     req.on('error', error => {
       reject(error);
     });
 
     req.write(data);
     req.end();
   });
 }
1 Like

I get the same error when posting to /oauth/token from my python script. According the links below, setting the content type to “application/x-www-form-urlencoded” in the header might solve your problem. Wasn’t that simple for me, unfortunately, I’m still stuck.

The grant_type looks correct. I don’t think this endpoint accepts JSON (even though it responds with JSON); try formatting your request body as a query string and declaring the application/x-www-form-urlencoded content type instead. Our application sends this header: Content-Type: application/x-www-form-urlencoded; charset=utf-8

Thanks for your answers. Got it working by following your instructions.

1 Like