google / google-api-javascript-client

Google APIs Client Library for browser JavaScript, aka gapi.
Apache License 2.0
3.14k stars 1.05k forks source link

gapi.client.init promise fails silently #680

Closed insomniac807 closed 3 years ago

insomniac807 commented 3 years ago

Possible duplicate here.

importing gapi as object using this script

import {
    gapi
} from 'gapi-script';
import oauth2Keys from './oauthKeys.json';
const APIKEYS = oauth2Keys.web;
const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest'];
const SCOPES = ["https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/youtube"];

//im building a vuejs3 application
mounted() {
    gapi.load('client:auth2', this.initClient);
},
    data() {
        return {
            GOOGLEAUTH: null
        }
    },
    methods: {
        initClient() {
            var self = this;
            console.log("initialising client...");
            gapi.client.init({
                discoveryDocs: DISCOVERY_DOCS,
                clientId: APIKEYS.client_id,
                scope: SCOPES
            }).then(function () {
                console.log(self);
                //listen for sign-in state changes
                self.GOOGLEAUTH = gapi.auth2.getAuthInstance();

                console.log("googleauth is....");
                console.log(self.GOOGLEAUTH);

                self.GOOGLEAUTH.isSignedIn.listen(self.updateSigninStatus);
                // Handle initial sign-in state. (Determine if user is already signed in.)
                self.updateSigninStatus(self.GOOGLEAUTH.currentUser.get());
            });
        }
}

Console logs in the promise callbacks not running. this.GOOGLEAUTH returning undefined in subsequent code. If I remove the clientid and scope params, the console logs run but this.GOOGLEAUTH still returning undefined.

dvanderb commented 3 years ago

Thanks for the issue report. We do not support using the linked module to load GAPI since it is a snapshot of the GAPI loading code which appears to be at least 8 months old at this point which results in loading old versions of the libraries. Please use officially documented methods of loading the libraries and try your use case again(samples: https://github.com/google/google-api-javascript-client/blob/master/docs/samples.md)