[Vuejs]-How to get access_token using vue-google-api package for futher use in Google api?

0👍

I did it, i have to init the client again, then call the picker in it’s own then:

            let GoogleAuth;
            let GoogleUser;
            let oauthToken;
            app.$gapi._libraryLoad('auth2') // Init new user to get access_token
            .then(auth2 => {
                auth2.init(
                    {
                        client_id: GoogleApiConfig.clientId,
                        scope: GoogleApiConfig.scope,
                    }
                ).then(() => {
                    GoogleAuth = auth2.getAuthInstance();
                    GoogleUser = GoogleAuth.currentUser.get();
                    oauthToken = GoogleUser.Zi.access_token; // Get access_token
                    // Must load the picker when in the promise so it has the permission
                    app.$gapi._libraryLoad('picker') // Call picker API after authentication
                    .then(picker => {
                        var newPicker = new google.picker.PickerBuilder()
                            .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
                            .setAppId(GoogleApiConfig.projectId)
                            .setOAuthToken(oauthToken)
                            .addView(new google.picker.DocsView().setIncludeFolders(true).setOwnedByMe(true)) // My Drive
                            .addView(new google.picker.DocsUploadView().setIncludeFolders(true)) // Upload view
                            .addView(new google.picker.DocsView(google.picker.ViewId.RECENTLY_PICKED)) // Recently picked
                            .addView(new google.picker.DocsView().setIncludeFolders(true).setOwnedByMe(false)) // Shared with me
                            .addView(new google.picker.DocsView().setIncludeFolders(true).setStarred(true).setLabel('Starred')) // Starred
                            .setDeveloperKey(GoogleApiConfig.apiKey)
                            .setCallback(data => {
                                // var fileId = data.docs[0].id;
                                // Contained in array when select multiple files
                                app.pickedIDs = data.docs;
                            })
                            .build();
                        newPicker.setVisible(true);
                    });
                }).catch(()=> {
                    // If not signed in
                    this.googleSignIn();
                });
            });

Leave a comment