如何实现Google Plus登录API

How to implement Google Plus Login API

本文关键字:Google Plus 登录 API 实现 何实现      更新时间:2023-09-26

我一直试图在我的html页面实现google +登录api很长一段时间了。到目前为止,我还没有取得任何成功。我已经能够渲染一个按钮作为gmail登录工作。然而,我不确定如何从谷歌+服务器获取用户信息。请检查下面的代码并告诉我如何获取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>

你的思路显然是对的。在您的signinCallback方法中,在确认用户已登录的第一个条件下,您可以对各种附加端点进行方法调用,以获取所需的信息。例如,这应该在块内工作:

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