0

I am trying to authenticate the user in order to get data to use to create or update meetings later. but it full of errors. Here I am sending Post Requests in order to get the AccessToken and then get the UserData as props.

export async function getServerSideProps(res){


const oauth = async() => {
     const zoomUserData = [];
       const b = Buffer.from(process.env.ZOOM_API_KEY + ":" + process.env.ZOOM_API_SECRET);

 const zoomRes = await fetch(`https://zoom.us/oauth/token?grant_type=authorization_code&code=${req.body.code}&redirect_uri=${process.env.ZOOM_REDIRECT_URL}`, {
   method: "POST",
   headers: {
     Authorization: `Basic ${b.toString("base64")}`,
   },
 });
 const zoomData = await zoomRes.json();
 const zoomUserRes = await fetch("https://api.zoom.us/v2/users/me", {
   method: "GET",
   headers: {
     Authorization: `Bearer ${zoomData.access_token}`,
   },
 });
 const zoomUserData = await zoomUserRes.json();


 /* 
   Encrypt and store below details to your database:
     zoomUserData.email
     zoomUserData.account_id
     zoomData.access_token
     zoomData.refresh_token
     zoomData.expires_in // convert it to time by adding these seconds to current time
 */



}



return{
     props:{zoomUserData}
   }
 }

and then i am passing the props to a page component like that :

export default function Meeting({zoomUserData}) { 
  const router = useRouter();  
      useEffect(() => {    
         if (router.query.code) { 
             fetch('/connectZoom',
              { method: 'POST',
         headers: {
           'ContType': 'application/json',
         },
         body: JSON.stringify({ code: router.query.code }),
       }).then(() => {
         console.log("success")
       }).catch(() => {
         console.log("No!")    
   });
     }
   }, [router.query.code]);
   console.log(zoomUserData)

   return (

     <a href={`https://zoom.us/oauth/authorize?response_type=code&client_id=${process.env.ZOOM_API_KEY}&redirect_uri=${process.env.ZOOM_REDIRECT_URL}`}>
       Connect Zoom
 </a>

   )
 }
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Hazem
  • 1
  • What error are you getting? – Willow Jan 24 '22 at 04:16
  • it returns undefined. although I use ngrok because Zoom doesn't return to localhost. – Hazem Jan 24 '22 at 04:19
  • What is returning `undefined` exactly? Are the requests to the Zoom endpoints returning the expected responses? – juliomalves Jan 24 '22 at 10:26
  • @juliomalves Any request under getserversideprops gets nothing! should I do anything regarding the API on any other page? I am running all of this code in one file "pages/zoom/meeting.js" – Hazem Jan 24 '22 at 19:26
  • Why do you have a function (`const oauth = async() => {...}`) wrapping all the logic inside `getServerSideProps`? That code won't run if you don't call the function. Just remove it, and run the code inside `getServerSideProps` directly. – juliomalves Jan 24 '22 at 19:29

0 Answers0