Skip to content

Cannot run gapi.client.init multiple times  #316

Closed

Description

Hi there,

I'm currently working on a angularJs dashboard composed of various widget.

The trouble I am facing is that when I call multiple widgets that use the gapi object, I end up with the following error (ie when I have connected to my gmail widget and I want to sign in to my calendar widget):

"gapi.auth2 has been initialized with different options. Consider calling gapi.auth2.getAuthInstance() instead of gapi.auth2.init()."

But the documentation states the following thing about this method:

https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientinitargs

"The gapi.client.init function can be run multiple times , such as to set up more APIs, to change API key, or initialize OAuth lazily."

Unless I misunderstood, this means that I should be able to run this method more than once.

Could you please help me ? How should I handle widgets that require to init the gapi client with specific client-id and discovery-doc ?

Thanks in advance for your help.

Below is a sample of my code, this is part of the directive used my calendar widget, but it the same code (except for the final request, upon requesting specific informations) that the gmail one (maybe this is the reason my widgets cannot work together):

//this function is called upon my directive has been loaded up
function handleClientLoad() {
    var loadGapiClient = new Promise(function(resolve) {
        gapi.load('client:auth2', resolve);
   });

   loadGapiClient
        .then(() => {
            //once the cient has been loaded, I can display and link my sign in button to the function that will handle the auth
            $scope.handleAuthClick = handleAuthClick;
            $scope.handleSignoutClick = handleSignoutClick;
            $scope.hide = false;
        });
}

//this is the function called when the user wants to sign in
function handleAuthClick() {
    initClient();
}

//this is the function that call the gapi.client.init with the CLIENT_ID, SCOPES  and DISCOVERY_DOCS that my widget is asking for
function initClient() {
    const CLIENT_ID = 'my-client-id';
    const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest'];
    const SCOPES = 'https://www.googleapis.com/auth/calendar.readonly';

    //this is where the gapi throws an error
    gapi
        .client
        .init({
            discoveryDocs: DISCOVERY_DOCS,
            clientId: CLIENT_ID,
            scope: SCOPES
        })
    .then(handleRequest, handleError);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions