Error exchanging code for access token


Format Your New Topic as Follows:

Description

Here’s my server.js code:

// const express = require("express");
const cors = require("cors");
const axios = require("axios");

const app = express();
const port = process.env.PORT || 3001;

app.use(cors());

async function exchangeCodeForAccessToken(authorizationCode) {
  const clientId = "#######";
  const clientSecret = "#####";
  const redirectUri =
    "https://zoom-itegration-backend.onrender.com/oauth-callback";

  const tokenUrl = "https://zoom.us/oauth/token";

  console.log("AUTHORIZATION CODE", authorizationCode);

  const requestBody = {
    grant_type: "authorization_code",
    code: authorizationCode,
    redirect_uri: redirectUri,
  };

  const authHeader = {
    Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString(
      "base64"
    )}`,
  };

  console.log("HELLOOOO OUTSIDE");

  try {
    const response = await axios.post(tokenUrl, requestBody, {
      headers: authHeader,
    });
    console.log("HELOOOOOO RESPONSE", response);

    return response.data;
  } catch (error) {
    console.error("Error exchanging code for access token:", error);
    throw error;
  }
}

app.get("/", (req, res) => {
  res.send({ message: "Hello Home!" });
});

app.get("/api/hello", (req, res) => {
  res.send({ message: "Hello from server!" });
});

app.get("/oauth-callback", async (req, res) => {
  // Handle the OAuth callback here
  const authorizationCode = req.query.code;
  console.log("CODEEEE", authorizationCode);
  const tokenResponse = await exchangeCodeForAccessToken(authorizationCode);
  // const accessToken = tokenResponse.access_token;

  res.send("OAuth callback complete. You can close this window.");
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

Error?

Error exchanging code for access token: AxiosError: Request failed with status code 400
Sep 29 01:56:10 PM at settle (/opt/render/project/src/backend/node_modules/axios/dist/node/axios.cjs:1913:12)
Sep 29 01:56:10 PM at IncomingMessage.handleStreamEnd (/opt/render/project/src/backend/node_modules/axios/dist/node/axios.cjs:2995:11)
Sep 29 01:56:10 PM at IncomingMessage.emit (events.js:388:22)
Sep 29 01:56:10 PM at endReadableNT (internal/streams/readable.js:1336:12)
Sep 29 01:56:10 PM at processTicksAndRejections (internal/process/task_queues.js:82:21) {
Sep 29 01:56:10 PM code: ‘ERR_BAD_REQUEST’,

What’s the exact issue here?

@souvikdeb26 ,

I’m doing something like this. There is a slight difference in the axios.post section.
Hope this helps

    const codeFromQueryString = 'myowncode';

    if (codeFromQueryString && codeFromQueryString.length>=1){
    //get the request token from zoom's oauth


    const url = 'https://zoom.us/oauth/token';
    const data = {
      code: codeFromQueryString,
      grant_type: 'authorization_code',
      redirect_uri: 'https://myredirecturl.cc/handletoken'
    };
    
    const headers = {
      Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
      'Content-Type': 'application/x-www-form-urlencoded'
    };
    
    axios.post(url, new URLSearchParams(data).toString(), { headers })
      .then(response => {

  
        console.log(response.data);
        res.status(200).json(response.data);
        // You can access the access_token in response.data.access_token
      })
      .catch(error => {
        console.error(error);
        res(error);
        // Handle any errors here
      });

    }