Google API 问题

Google APIs Issue

我正在尝试在我的网站上实现 "signin with google" 按钮。

我尝试了 javascript:

<script>
    gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({
            client_id: 'MY_CLIENT_ID.apps.googleusercontent.com',
            fetch_basic_profile: false,
            scope: "email" //I do only need the email for the moment
        });

        auth2.signIn().then(function() {
            console.log(auth2.currentUser.get().getId());
        });
    });
</script>

和"non-javascript":

<meta name="google-signin-scope" content="email">
<meta name="google-signin-client_id" content="MY_CLIENT_ID.apps.googleusercontent.com">

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

<script>
    function google(googleUser) {
        var profile = googleUser.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());

        var id_token = googleUser.getAuthResponse().id_token;
        console.log("ID Token: " + id_token);
    }

    function fail(error) {
        console.log(error);
    }
</script>

<div class="g-signin2" data-onsuccess="google" data-onfailure="fail" data-theme="dark" data-longtitle="true"></div>

版本。


但在这两种情况下我都收到此错误(第一个脚本中的错误和第二个脚本中 fail() 的日志):

TypeError: this.u3.open is not a function
    at fv.open (cb=gapi.loaded_0:250)
    at gw (cb=gapi.loaded_0:298)
    at Dx (cb=gapi.loaded_0:341)
    at $w (cb=gapi.loaded_0:337)
    at xx.<anonymous> (cb=gapi.loaded_0:338)
    at new _.rk (cb=gapi.loaded_0:188)
    at xx.Ej (cb=gapi.loaded_0:338)
    at bx.a.<computed> [as signIn] (cb=gapi.loaded_0:321)
    at login:269
    at platform.js:28

我已经在 API 开发人员控制台中将我的域列入白名单...我做错了什么?

通过将 ux_mode: "redirect" 添加到 initializator/meta 标签解决

变成这样:

gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({
            client_id: 'MY_CLIENT_ID.apps.googleusercontent.com',
            fetch_basic_profile: false,
            scope: "email",
            ux_mode: "redirect"
        });

        auth2.signIn().then(function() {
            console.log(auth2.currentUser.get().getId());
        });
    });

问题是现在我无法调用 fail()success()

我知道这是题外话,但如果您在评论中或 here 中提供任何帮助,我们将不胜感激!