Web应用程序Google登录

时间:2017-03-17 11:11:35

标签: javascript html google-signin

我正在我的网络应用中集成谷歌登录。我按照here的说明进行操作。但我无法从登录中获取任何用户信息。登录按钮的工作非常精细。

<meta name="google-signin-client_id" content="3*****-c*******************.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>

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

我使用这些javascript函数:

if (auth2.isSignedIn.get()) {
        var profile = auth2.currentUser.get().getBasicProfile();
        console.log('ID: ' + profile.getId());
        console.log('Full Name: ' + profile.getName());
        console.log('Given Name: ' + profile.getGivenName());
        console.log('Family Name: ' + profile.getFamilyName());
        console.log('Image URL: ' + profile.getImageUrl());
        console.log('Email: ' + profile.getEmail());
        email=profile.getEmail();
        }
    }

此函数导致错误:未定义auth2。 如果我在函数之前使用window.onLoadCallback = function(),那么它不会给出错误,也不会在日志中提供任何内容。如果我只使用这个:

    function onSignIn(googleUser) {
        var id_token = googleUser.getAuthResponse().id_token;
        console.log("done");
    }

我再次收到错误,但控制台日志中没有任何错误。

1 个答案:

答案 0 :(得分:1)

将此代码置于if(auth2.isSignedIn.get())之前。尝试运行它。

gapi.load('auth2', function () {
    auth2 = gapi.auth2.init();

    // Sign the user in, and then retrieve their ID.
    auth2.signIn().then(function () {
        console.log(auth2.currentUser.get().getId());
    });
});
相关问题