javascriptnode.jsexpresspassport-google-oauthgoogle-auth-library

Error saving code: GaxiosError: invalid_request when using passport-google


this my post request

app.post('/auth/google', async (req, res) => {
    try {
        const {
            code
        } = req.body;
      
    } catch (error) {
       
    }
});

i'm getting the token from my front end

 4/0AbUR2VMmkydX1bHZeUIq6xm8558a8EyxdNhc0q2ouILfp2Cc3gL3mSd3w83Qn6JJ4jaqdg

please how can i verify the code and get the user details using google-auth-library

const verifyGoogleAccessToken = async (accessToken) => {
    oauth2Client.setCredentials({
        access_token: accessToken
    });

    const userinfo = await oauth2Client.request({
        url: "https://www.googleapis.com/oauth2/v3/userinfo",
    });

    return userinfo.data;
};

please i need help,i've been on this for days


Solution

    1. Call the Google SDK from the frontend.
    2. Extract the code or access token and send to your backend for verification.
    3. Use your backend Google api to verify the code or token.
    4. If verified, sign them in the backend and then send a response to frontend
    const express = require('express');
    const axios = require('axios');
    const cors = require('cors');
    
    const { OAuth2Client } = require('google-auth-library');
    const oauth2Client = new OAuth2Client()
    
    const app = express();
    
    // Enable CORS for all routes
    app.use(cors());
      app.post('/auth', async (req, res) => {
        try {
          const code = req.headers.authorization;
          console.log('Authorization Code:', code);
    
          // Exchange the authorization code for an access token
          const response = await axios.post(
            'https://oauth2.googleapis.com/token',
            {
              code,
              client_id: '58730156701-d27fqgjb0.apps.googleusercontent.com',
              client_secret: 'GOCSPX-u02eNiucPXxRAsQVi',
              redirect_uri: 'postmessage',
              grant_type: 'authorization_code'
            }
          );
          const accessToken = response.data.access_token;
          console.log('Access Token:', accessToken);
    
          // Fetch user details using the access token
          const userResponse = await axios.get(
            'https://www.googleapis.com/oauth2/v3/userinfo',
            {
              headers: {
                Authorization: `Bearer ${accessToken}`
              }
            }
          );
          const userDetails = userResponse.data;
          console.log('User Details:', userDetails);
    
          // Process user details and perform necessary actions
    
          res.status(200).json({ message: 'Authentication successful' });
        } catch (error) {
          console.error('Error saving code:', error);
          res.status(500).json({ message: 'Failed to save code' });
        }
      });
    
    
    app.listen(4000, () => {
        console.log('Server running on port 4000');
    });