how to get google classroom info with firebase signIn in svetle&sveltekit

34 Views Asked by At

I coded signIn with firebase auth

async function signIn() : Promise<UserCredential> {
  return await new Promise((resolve, reject) => {
    signInWithPopup(auth, provider)
      .then(async (result) => {
        resolve(result);
      }).catch((err) => {
        reject(err);
      });
  });
}

signIn().then(async (result: UserCredential) => {...})
...

I want to get google Classroom info of signined user. So I tried like

const courses = await (await fetch('https://classroom.googleapis.com/v1/courses', {
    headers: {
      'Authorization': `Bearer ${access_token}`
    }
  })).json();
  console.log("a-courses", courses);

but I knew access_token is expired after 1 hours. so how to refresh access token?

I tried like :

signIn().then(async (result: UserCredential) => {
     onAuthStateChanged(auth, async (user: User | null) => {
          // save user?.refreshToken in DB
        });
});

sent user.refresh_token in DB, to api\user\refresh

api\user\refresh\server.ts

console.log((await (await fetch('https://oauth2.googleapis.com/token', {
        method: "POST",
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            "client_id": "MY_CLIENT_ID",
            "client_secret": "MY_CLIENT_SECRET",
            "refresh_token": req.refresh_token,
            "grant_type": "refresh_token",
        })
    })).json()));

but it doesn't work; with this log

{ error: 'invalid_grant', error_description: 'Bad Request' }
0

There are 0 best solutions below