简体   繁体   中英

How to implement Google Plus Login API

I have been trying to implement google plus login api in my html page for a long time now. I have not had any success so far. I have been able to render a button to work as a gmail login. However, I am not sure how to get the user info from google plus servers. Please inspect my code below and advise me how to get userinfo..

              <script type="text/javascript">
              (function() {
                var po = document.createElement('script');
                po.type = 'text/javascript'; po.async = true;
                po.src = 'https://apis.google.com/js/client:plusone.js?onload=render';
                var s = document.getElementsByTagName('script')[0];
                s.parentNode.insertBefore(po, s);
                signinCallback(authResult);

              })();

              function render() {
                gapi.signin.render('gp', {
                  'callback': 'signinCallback()',
                  'clientid': 'I know my id goes here :P.apps.googleusercontent.com',
                  'cookiepolicy': 'single_host_origin',
                  'requestvisibleactions': 'http://schema.org/AddAction',
                  'scope': 'https://www.googleapis.com/auth/plus.login'
                });
              }

              function signinCallback(authResult) {
                  if (authResult['status']['signed_in']) {
                    // Update the app to reflect a signed in user
                    // Hide the sign-in button now that the user is authorized, for 

                  } else {
                    // Update the app to reflect a signed out user
                    // Possible error values:
                    //   "user_signed_out" - User is signed-out
                    //   "access_denied" - User denied access to your app
                    //   "immediate_failed" - Could not automatically log in the user
                    console.log('Sign-in state: ' + authResult['error']);
                  }
                }
              </script>

You're clearly on the right track. In your signinCallback method, in the first condition where you've confirmed the user is signed in, you're able to make the method calls to the various plus endpoints to get the information you need. For example, this should work inside that block:

gapi.client.plus.people.get({
  'userId' : 'me'
}).execute(function(resp) {
  console.log('Display Name: ' + resp.displayName);
});

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