【问题标题】:Why do I keep getting a auth2 is not defined? [duplicate]为什么我不断收到未定义的 auth2? [复制]
【发布时间】:2016-12-18 16:04:12
【问题描述】:

我正在尝试将 google 签名按钮集成到我的网络应用程序中。这是我第一次这样做,我在控制台日志中不断收到以下消息...

auth2 未定义

此外,当我刷新页面时,Google 按钮显示“登录”而不是“已登录”

下面是我的代码。谢谢!

 <script>


    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());
        });




    });


    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());
}


      function onSignIn(googleUser) {
        // Useful data for your client-side scripts:
        var profile = googleUser.getBasicProfile();
        console.log("ID: " + profile.getId()); // Don't send this directly to your server!
        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());

        // The ID token you need to pass to your backend:
        var id_token = googleUser.getAuthResponse().id_token;
        console.log("ID Token: " + id_token);
      }











    </script>

【问题讨论】:

  • 您能否展示一下您是如何使用 api.js 或 platform.js 添加加载 gapi 的脚本标签的?

标签: javascript google-signin


【解决方案1】:

不要让 Google API 误导您。错误可能是因为这一行(并且代码处于('严格模式'):

auth2 = gapi.auth2.init();

因为错误指出未定义“auth2”(并且在严格模式下不允许在全局空间中创建变量)。声明如下:

var auth2 = gapi.auth2.init();

gapi auth2 init failing with "Uncaught ReferenceError: auth2 is not defined" 也发生了同样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2020-04-30
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多