如何调用谷歌的getBasicProfile()到谷歌登录只有按钮点击? [英] how to call getBasicProfile() of google to google signin on only button click?

查看:799
本文介绍了如何调用谷歌的getBasicProfile()到谷歌登录只有按钮点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在Google网站上登录Google:

 < script src =https://apis.google.com/js/platform.jsasync defer>< / script> 

< script>
函数onSignIn(googleUser){
var profile = googleUser.getBasicProfile();
//console.log('ID:'+ profile.getId()); //不要发送到你的后端!改用ID标记。
//console.log('Image URL:'+ profile.getImageUrl());
//console.log('Name:'+ profile.getName());
//console.log('Email:'+ profile.getEmail());
var user_uname = profile.getName();
var user_email = profile.getEmail();
alert(user_uname);
}

< / script>

以下是登录Google的按钮:

 < div class =g-signin2data-onsuccess =onSignIn>< / div> 

我想给用户google登录,但问题出在页面加载时onSignIn()函数被调用自动。
我只需要点击按钮。任何人都可以帮助我?

解决方案

最佳解决方案是仅在用户未登录时呈现登录按钮。

 < html> 
< head>
< meta name =google-signin-client_idcontent =YOUR_CLIENT_ID>
< / head>
< body>
< script>
函数onSignIn(googleUser){
var profile = googleUser.getBasicProfile();
var user_name = profile.getName();
alert(user_name);


函数onLoad(){
gapi.load('auth2,signin2',function(){
var auth2 = gapi.auth2.init() ;
auth2.then(function(){
//当前值
var isSignedIn = auth2.isSignedIn.get();
var currentUser = auth2.currentUser.get() ;

if(!isSignedIn){
//渲染g-signin2按钮
gapi.signin2.render('google-signin-button',{
'onsuccess':'onSignIn'
});
}
});
});
}
< / script>

< div id =google-signin-button>< / div>

< script src =https://apis.google.com/js/platform.js?onload=onLoadasync defer>< / script>
< / body>
< / html>


i have take google signin in my website :

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

<script>
    function onSignIn(googleUser) {
      var profile = googleUser.getBasicProfile();
      //console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
      //console.log('Image URL: ' + profile.getImageUrl());
      //console.log('Name: ' + profile.getName());
      //console.log('Email: ' + profile.getEmail());
      var user_uname = profile.getName();
      var user_email = profile.getEmail();
      alert(user_uname);
    }

</script>

and here is a button to login google:

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

i want to give user google signin but the problem is whenever page is load onSignIn() function is called automatically. i want it only on button click. can anybody help me?

解决方案

Best solution is to render sign-in button only when user is not signed in.

<html>
<head>
   <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
</head>
<body>
  <script>
    function onSignIn(googleUser) {
      var profile = googleUser.getBasicProfile();
      var user_name = profile.getName();
      alert(user_name);
    }

    function onLoad() {
      gapi.load('auth2,signin2', function() {
        var auth2 = gapi.auth2.init();
        auth2.then(function() {
          // Current values
          var isSignedIn = auth2.isSignedIn.get();
          var currentUser = auth2.currentUser.get();

          if (!isSignedIn) {
            // Rendering g-signin2 button.
            gapi.signin2.render('google-signin-button', {
              'onsuccess': 'onSignIn'  
            });
          }
        });
      });
    }
  </script>

  <div id="google-signin-button"></div>

  <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</body>
</html>

这篇关于如何调用谷歌的getBasicProfile()到谷歌登录只有按钮点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆