简体   繁体   中英

Google login for server-side apps

I'm following the Google guide for signing in with Google and sending the access code to a server:

Guide here: https://developers.google.com/identity/sign-in/web/server-side-flow#step_6_send_the_authorization_code_to_the_server

I have two really simple questions:

  • How does the closure in the jQuery click event know about auth2 ? If you run this code in Chrome, you'll get an error saying auth2 is undefined. How would I go about fixing that error?

Run from onload:

function start() {
    gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({...});
    });
}

jQuery click handler

(function($) {
    $('#signinButton').click(function() {
        auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(signInCallback);
    });
})(jQuery);
  • How is signInCallback passed information? Is this part of the callback process?

The code:

function signInCallback(authResult) {
    if(authResult['code']) {
        $.ajax({...});
    } else { ... }
}

Loading the client api at the top of the HTML file:

 <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>

and then initializing:

function start() {
    gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({...});
    });
}

also in the header will do the trick. The guide says this, but neglects to state that it won't really work otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM