29

There is a bug in my code. I am able to sign in and retrieve user information. But the signinCallback is called again(I don't know how). And it shows User information that I had earlier is gone! Here is the HTML side:

<span id="signinButton">
    <span
        class="g-signin"
        data-callback="signinCallback"
        data-clientid="CLIENT_ID"
        data-cookiepolicy="single_host_origin"
        data-requestvisibleactions="http://schemas.google.com/AddActivity"
        data-scope="https://www.googleapis.com/auth/plus.profile.emails.read"
        data-width="standard"
        data-height="short">
    </span>
</span>

and here is the javascript side:

var AuthStates = {
  google: null
};

function signinCallback(authResult) {
    console.dir(authResult);
    console.log('Sign-in state: ' + authResult['error']+authResult['access_token']);
    AuthStates.google = authResult;
    console.log('signinCallback');
    chooseAuthProvider();
}

function chooseAuthProvider() {
  if (AuthStates.google && AuthStates.facebook) {
    if (AuthStates.google['access_token']) {
      // Signed in with Google, you can now use Google+ APIs.
      console.log(AuthStates.google);
      gapi.client.load('plus','v1', function(){
        var request = gapi.client.plus.people.get({
          'userId': 'me'
        }); 
        request.execute(function(resp) {
          document.getElementById('cname').value =resp.displayName;
          document.getElementById('cemail').value =resp.emails[0].value;
          console.log('Retrieved profile for:' + resp.displayName + ' ' + resp.emails[0].value);
        });
      });
}
}

It gives this response to the console on the second signinCallback

Sign-in state: user_signed_outundefined
signinCallback

Result of console.dir(authResult)

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
user3528581
  • 311
  • 2
  • 4
  • I'm having a very similar issue [here](http://stackoverflow.com/questions/24056169/google-signincallback-called-twice-when-user-is-signed-out). I also posted on [Google + Dev Community](https://plus.google.com/communities/113527920160449995981). Could you please give us a screenshot of what `console.dir(authResult)` outputs, right after signInCallback is called ? Specifically, what is the value of `authResult['status']['method']` in both calls ? – Waldo Jeffers Jun 10 '14 at 12:49
  • authResult['status']['method']=null – user3528581 Jun 10 '14 at 13:23
  • Well, we have the same problem then. Are you using multiple Google Accounts ? – Waldo Jeffers Jun 10 '14 at 13:25
  • Yes,I am using multiple accounts. Btw the screenshot is added in my question. Are u going to report this? – user3528581 Jun 10 '14 at 13:51
  • I was waiting to see if someone had a reasonable explanation/solution for this on Google+ Dev Community. If none has been provided tomorrow, I'll report this issue and paste the link here, is it OK with you ? – Waldo Jeffers Jun 10 '14 at 15:02
  • Yeah do so, and fast! But I don't know how and why, but i think its working fine for now(not making the second callback). I'll let you know if it returns. Check urs in the mean time! – user3528581 Jun 11 '14 at 04:22
  • [Here you go](https://code.google.com/p/google-plus-platform/issues/detail?id=880&thanks=880&ts=1402474067). I'm having the exact same problem as before (and did not change anything in my code). Did you change something (erased cookies etc...) ? – Waldo Jeffers Jun 11 '14 at 08:09
  • Nothing really. maybe changed the data-scope data-scope=" https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login " But this was causing the same problem as the previous one. So nothing on my part. This API is whimsical. – user3528581 Jun 11 '14 at 08:42
  • I'm also having this issue. My sign in callback gets called twice for no apparent reason. Even by following the JavaScript Sign In Flow shown by google: https://developers.google.com/+/web/signin/javascript-flow – lpdahito Jun 11 '14 at 22:43
  • I copy pasted their example earlier, and it was giving the same problem.Hope they fully solve this! – user3528581 Jun 12 '14 at 06:11
  • Any reply back from those guys? – user3528581 Jun 14 '14 at 18:12
  • No, I am not getting a lot of attention from Google right now...But you can back my vote for my issue or report one. I guess the more we are, the quicker they'll tackle the issue [Link to my issue report](https://code.google.com/p/google-plus-platform/issues/detail?id=880&thanks=880&ts=1402474067) [Link to my post on Google + Dev Community](https://plus.google.com/105677613146028971470/posts/JhU1rtSMnKQ) [Link to my SO post](http://stackoverflow.com/questions/24056169/google-signincallback-called-twice-when-user-is-signed-out?noredirect=1#comment37126851_24056169) – Waldo Jeffers Jun 19 '14 at 16:43
  • possible duplicate of [Google Login Hitting Twice?](http://stackoverflow.com/questions/23020733/google-login-hitting-twice) – Ben Smith Oct 01 '14 at 10:31
  • A way to get around this is to revoke access to the token that is giving you problems. When you log in it seems to work again. However, it seems that there is an expiry. When ever it expires this behavior starts again. – ADL Mar 09 '15 at 18:41
  • 1
    there is an answer for that already http://stackoverflow.com/questions/23020733/google-login-hitting-twice?lq=1 – Dan Kuida Jun 10 '15 at 16:37
  • As @DanKuida says I have an answer for this problem here http://stackoverflow.com/a/23096017/203371 – Ben Smith Jun 10 '15 at 22:03
  • I have some questions it could help me to get an answer for your problem Does the second call appears when you enter the right user information? Does the second call appears when you when fields are empties? if not what appears? Does this (Sign-in state: user_signed_outundefined signinCallback) appears when you enter the right user information? What appears in the console.log as error result for each of the above? – Mohammed Moustafa Jun 10 '15 at 22:04
  • Does this (Sign-in state: user_signed_outundefined signinCallback) shows up by it self or when you do some action in the second call? and does it goes away if you try to enter user information even if it's correct or wrong? if it does(on which)? Did you provide a complete code for function chooseAuthProvider()or you cut it out? Does this problem is the same on all the browsers? if not please provide information for each. – Mohammed Moustafa Jun 10 '15 at 22:05

3 Answers3

2

Try updated instructions at "Integrating Google Sign-In into your web app" page.

Valentin Podkamennyi
  • 7,161
  • 4
  • 29
  • 44
1

Your call to request.execute() in your callback method is causing the callback method to be re-triggered with "user_signed_out" value in the error property.

If you take a look at the Google documentation "Signing out the user" it reads:

When the user refreshes the page or navigates to another part of your website, the callback will fire with user_signed_out value in the error property until the user clicks the sign-in button again.

Hence I believe it is your call to request.execute() which is triggering the second call to the callback method.

You can guard against this second call to the callback by putting a condition within the callback method e.g.

function signinCallback(authResult) {
 if (authResult['status']['signed_in']) {
  console.dir(authResult);
  console.log('Sign-in state: ' + authResult['error']+authResult['access_token']);
  AuthStates.google = authResult;
  console.log('signinCallback');
  chooseAuthProvider();
 }
}

See Google's documentation on "Monitoring the user's session state" for an example of the previously mentioned guard conditions.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
0

This might be helpful for you

(function() { var GOOGLE_PLUS_SCRIPT_URL = 'https://apis.google.com/js/client:plusone.js';

 window.oauth2Callback = function(authResult) {
 if (authResult['access_token']) {
  accessToken = authResult['access_token'];
  $(function() {
$.getScript(GOOGLE_PLUS_SCRIPT_URL);}