我想在点击Google Plus按钮时调用Google Plus回调函数

ABo*_*rty 8 javascript jquery codeigniter google-login google-plus-one

我在我的项目[内置CodeIgniter]中使用了Google Plus按钮.这里我添加了以下代码.

<span id="signinButton">
  <span
    class="g-signin gooConnect"
    data-callback="signinCallback"
    data-clientid="my_project_client_id"
    data-cookiepolicy="single_host_origin"
    data-requestvisibleactions="http://schemas.google.com/AddActivity"
    data-scope="https://www.googleapis.com/auth/userinfo.email">
  </span>
</span>
Run Code Online (Sandbox Code Playgroud)

然后我添加了Google提供的Javascript代码.

<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';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();

  function signinCallback(authResult) {
    if (authResult['access_token']) {
      $.ajax({
        url:base_url+'index.php/user/getUserProfile',
        type:'POST',
        data:{'access':authResult['access_token']},
        beforeSend  : function(){
          $("#loadingImageBeforeResult").show('slow');
        },
        success : function(resp){
          $("#loadingImageBeforeResult").hide('slow');
          if( resp == 'exist' ){
            window.location.href=base_url+'index.php/user/my_deals';
          } else {
            $('#link_for_geniepage').trigger('click');
          }
        },
        error : function(resp){}
      });
    } else if (authResult['error']) {
      // There was an error.
      // Possible error codes:
      //   "access_denied" - User denied access to your app
      //   "immediate_failed" - Could not automatially log in the user
      // console.log('There was an error: ' + authResult['error']);
    }
  }
</script> 
Run Code Online (Sandbox Code Playgroud)

这对我来说很好,但是如果我在单独的标签中登录我的Gmail帐户然后转到我的登录页面,则回调功能只会使用我的Gmail凭据自动登录并将我重定向到我的信息中心.

我希望除非我点击Google Plus按钮,否则回调功能不起作用.我怎样才能做到这一点?请帮我.

Ron*_*ero 2

从文档来看,以这种方式使用登录按钮时,它看起来总是会尝试立即验证。由于您已经登录 Google 并已授权该应用程序,因此 Google 会自动让您登录并将您发送到仪表板。

我建议不要使用该示例代码。相反,您可以使用 Google Javascript API 的其他部分 ( https://developers.google.com/+/web/api/javascript ) 创建一个普通按钮的 Google 登录按钮。单击该按钮后,调用gapi.auth.authorize()以登录用户。然后,直到他们单击该按钮为止,什么也不会发生,当他们单击该按钮时,它要么要求批准/登录,要么只是自动让用户登录。