CapacitorGoogleAuth icon indicating copy to clipboard operation
CapacitorGoogleAuth copied to clipboard

No `refreshToken ` at first login

Open Saqib92 opened this issue 1 year ago • 0 comments

I need refreshToken at the time of login (Web, Android, IOS) in all the platforms. I need to use some google API's. Specifically spreadsheet API to read and write data. Currently, using access_token is working fine but it expires after 1 hour. i need to updated access_token.

My backend nodejs:

const { google } = require('googleapis');

app.post("/test", (req, res) => {
    refreshAccessToken(req.body.refreshToken); // but this is working for access_token not for idToken and there is no refreshToken
    res.json({ updated_token: "world" });
});


async function refreshAccessToken(refreshToken) {
    const OAuth2 = google.auth.OAuth2;
    const CLIENT_ID = '';
    const CLIENT_SECRET = ''
    const REDIRECT_URI = 'https://developers.google.com/oauthplayground';

    const oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
    oauth2Client.setCredentials({ refresh_token: refreshToken });

    try {
        const newCredentials = await oauth2Client.refreshAccessToken();
        oauth2Client.setCredentials(newCredentials);
        console.log('newCredentials', newCredentials);
        //console.log('Access token refreshed successfully:', newCredentials.access_token);
        // Use the updated access token for your API requests
    } catch (err) {
        console.error('Error refreshing access token:', err);
    }
}

Saqib92 avatar Sep 09 '24 15:09 Saqib92