4

I have integrated my Google drive using Oauth2 in server side and stored credentials like access token and refresh token in data base.

        const clientId = `${process.env.GOOGLE_DRIVE_CLIENT_ID}`;
        const clientSecret = `${process.env.GOOGLE_DRIVE_CLIENT_SECRET}`;
        const redirectURI = `${process.env.SERVERHOST}/connect/gdrive`;
        const oAuth2Client = new google.auth.OAuth2(
            clientId,
            clientSecret,
            redirectURI,
        );
        const authUrl = oAuth2Client.generateAuthUrl({
            access_type: "offline",
            scope: SCOPE,
        });
        return res.send({ url: authUrl });

Once user authorises the app, next time when ever user loads google picker, it should be open directly without auth screen. To achieve this I am retrieving access token from my db(if it is expired I am generating new token from refresh token) and passing it on to google picker

createPicker(accessToken) {
        const { gdriveOAuth2Token } = this.props.userInfo;
        const appId = '';
            console.log('acc : ', accessToken);
            const uploadView = new google.picker.DocsUploadView();
            var picker = new google.picker.PickerBuilder().
                    addViewGroup(
                            new google.picker.ViewGroup(google.picker.ViewId.DOCS).
                            addView(google.picker.ViewId.DOCUMENTS).
                            addView(google.picker.ViewId.PRESENTATIONS)).
                            addView(uploadView).
                            setAppId(appId).
                            setOAuthToken(accessToken).
                            setDeveloperKey(developerKey).
                            setCallback(this.pickerCallback).
                            build();
            picker.setVisible(true);
    }

It works fine for a particular session. However, for new sessions it asks for user name and password again. I don't want user to go through authentication screen once he has integrated his drive.

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
ot954
  • 425
  • 5
  • 19
  • How is your application being run? Is this on localhost for testing or are you running in prod off server? – Rafa Guillermo Jan 06 '20 at 16:22
  • @RafaGuillermo running on localhost – ot954 Jan 07 '20 at 04:05
  • What are your SCOPES? – Rafa Guillermo Jan 08 '20 at 15:01
  • @RafaGuillermo ['https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/userinfo.email'] are my scopes – ot954 Jan 09 '20 at 04:27
  • How are you getting the access token? When the session is reloaded the access token needs to be re-obtained from your database, but how is it you are identifying this? With a session reset the user would have to be identified again... unless you're retrieving something from cache? – Rafa Guillermo Jan 09 '20 at 09:28
  • Hello @ot954, I am having the same issue, How did you manage to solve this and avoid user having to signing in over and over? – lucianov88 Jan 28 '21 at 23:46
  • I had this issue, in my case it turned out that I didn't add the required scope at the time of creating the oauth url in the server side. Like, although I had set up the required scopes in google developer console, I forgot to add the required scope in server side code – Shaikh Amaan FM Apr 17 '22 at 21:42

0 Answers0