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

风格不统一 提交于 2021-01-01 07:03:53

问题


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.

来源:https://stackoverflow.com/questions/59609728/is-it-possible-to-open-google-picker-with-access-token-which-is-fetched-from-ser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!