Google+ oauth2 callback fires multiple times

I have the following piece of code:

var myParams = { 
'clientid' : 'XXXXX.apps.googleusercontent.com', 
'cookiepolicy' : 'single_host_origin', 
'callback' : _.bind(function(response){ this._loginGoogleCb(response); }, this), 
            'scope' : 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.emails.read', 
            'requestvisibleactions' : 'http://schemas.google.com/AddActivity' 
        }; 
gapi.auth.signIn(myParams); 

      

The first time I click the button and this code runs, no problem.
But when I click the button a second time, the callback (and only the callback) is executed twice. Every time I execute this code, the number of requests to the google server (and its associated callback) increases by 1.

I checked twice, the calling function itself only runs once when iterated over.
The button itself is not a problem.

Any idea what the problem is?

+3


source to share


1 answer


As you can see from this article , there are three different ways to determine the state:

  • NULL
  • Suggest
  • AUTO

in the authorization object:

{
  "id_token": string,
  "access_token": string,
  "expires_in": string,
  "error": string
  "status": { /* object */
    "google_logged_in" : boolean,
    "signed_in" : boolean,
    "method" : string /* null, PROMPT, or AUTO */
  }
}

      



What happens when the first time you log in, only one of these status methods ("PROMPT") is triggered, but when the button is pressed again, two status methods ("PROMPT" and "AUTO") are triggered.

Sample "signinCallback" code to resolve these state methods can be found here .

Also, if you call the underscore bind funtion multiple times, the associated function will also be called multiple times. Hence, the reason you see "the number of requests to the google server (and the associated callback) is increasing from 1." I would suggest encapsulating this call for binding in another function and include a guard condition to stop it being called multiple times.

+2


source







All Articles