Search code examples
javascriptgoogle-apigoogle-oauthgoogle-signin

GoogleUser object does not have grantOfflineAccess method?


I am using Google Sign-in to authenticate users on my website and then as a separate step asking for offline permissions.

According to the documentation, the GoogleUser object should have a method "grantOfflineAccess" which prompts for additional permissions without prompting the user to confirm their account. However inspecting the object in Firebug, I find all the other methods described but not grantOfflineAccess.

I have a workaround using the GoogleAuth object's grantOfflineAccess method but that forces the user to confirm their account (which I would like to avoid, as they have just performed that step during login). I would like to keep the login and authorize offline access prompts separate so I can do some validation between them.

Is the documentation wrong/outdated? Is there another way to get my desired behaviour?


Solution

  • I just checked it out myself, the method really doesn't exist. I think this is actually an error in the docs.

    So I poked around a little bit and found another way to achieve what you want: There is an (apparently undocumented) parameter named authuser. It's basically the index of the account you are logged in (0 for the first, 1 for the second, ...). Google uses this internally for stuff like GoogleDocs etc. After some more poking, I found the authuser in the GoogleUser data:

    enter image description here

    Turns out that wc is the getAuthResponse() data, so you can access this index with: gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().session_state.extraQueryParams.authuser.

    You can now call the grantOfflineAccess of GoogleAuth with this parameter, resulting in the following call:

    var auth = gapi.auth2.getAuthInstance();
    var user = auth.currentUser.get();
    auth.grantOfflineAccess({
      authuser: user.getAuthResponse().session_state.extraQueryParams.authuser
    });
    

    This will open the prompt without an account chooser :-) Hope I could help!