首页 > 解决方案 > 在使用 ux_mode 时获取用户信息:'redirect' 使用自定义图像进行 google 登录

问题描述

我想使用ux_mode:redirect而不是使用 javascript 进行 google 登录的默认弹出方法。目前登录发生如下,

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


<script>
function onLoadGoogleCallback(){

   element = document.getElementById('glogin');

   //console.log(element);

    gapi.load('auth2', function() {
      auth2 = gapi.auth2.init({
        client_id: 'CLIENT_ID.apps.googleusercontent.com',
        cookiepolicy: 'single_host_origin',
         scope: 'profile',
        //ux_mode : 'redirect'
      });

     auth2.attachClickHandler(element, {},
       function(googleUser) {
           console.log('Signed in: ' + googleUser.getBasicProfile().getName());
           var profile = googleUser.getBasicProfile();
           console.log('Name: ' + profile.getName());
           console.log('Image URL: ' + profile.getImageUrl());
           console.log('Email: ' + profile.getEmail());
         }, 
         function(error) {
           console.log('Sign-in error', error);
         }
       );
    });



}
</script>

其中glogin是我用于 google 登录的 img 的 id。

<img src="./img/login-g.jpg" id="glogin" >

这工作正常,我在控制台中获取信息。但是如果我尝试重定向方法,控制台中不会显示任何内容。登录后我用什么代替 auth2.attachClickHandler(...)来获取信息?

标签: javascriptoauthgoogle-signin

解决方案


推荐阅读