Is it possible to open google picker with access token which is fetched from server side Oauth2?
Asked Answered
U

0

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.

Ultrastructure answered 6/1, 2020 at 9:39 Comment(7)
How is your application being run? Is this on localhost for testing or are you running in prod off server?Ellerd
@RafaGuillermo running on localhostUltrastructure
What are your SCOPES?Ellerd
@RafaGuillermo ['googleapis.com/auth/drive.file', 'googleapis.com/auth/userinfo.email'] are my scopesUltrastructure
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?Ellerd
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?Minta
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 codePartible

© 2022 - 2025 — McMap. All rights reserved.