Initiate Google sign in on button click

↘锁芯ラ 提交于 2019-12-12 10:33:51

问题


Hello I am using google api to sign in to my web application . Now Its working fine . But the problem is the sigin is initiated automatically as I go to my log in page . But I do not want that . I want the user to click the signin button first then the process will start .

I am using below button

<div class="g-signin2" data-onsuccess="onSignIn"></div>

and google log in references .


回答1:


Use 'attachClickHandler' to Initiate Google Sign In.

<script src="https://apis.google.com/js/client:platform.js?onload=init" async defer></script>
<script>
   function init() {
        gapi.load('auth2', function() {
            auth2 = gapi.auth2.init({
                client_id: '*************.apps.googleusercontent.com',
                cookiepolicy: 'single_host_origin',
                scope: 'profile email'
            });
            element = document.getElementById('glogin');
            auth2.attachClickHandler(element, {}, onSignUp, onFailure);
        });
    }
    function onSignUp(googleUser) {
      var profile = googleUser.getBasicProfile();
    }
</script>



回答2:


Best way is to use Java Script to sign in to google as I have found out .

use

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

to initialize js . And finally call below method to sign-in user

function login() 
{
  var myParams = {
    'clientid' : 'YOUR_CLIENT_ID.apps.googleusercontent.com', //You need to set client id
    'cookiepolicy' : 'single_host_origin',
    'callback' : 'loginCallback', //callback function
    'approvalprompt':'force',
    'scope' : 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read'
  };
  gapi.auth.signIn(myParams);
}

By using this I was able to solve this problem . Now User needs to click on sign-in to login



来源:https://stackoverflow.com/questions/32624007/initiate-google-sign-in-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!