【问题标题】:gapi.auth.authorize "Cannot read property 'authorize' of undefined"gapi.auth.authorize “无法读取未定义的属性‘授权’”
【发布时间】:2017-12-30 05:30:59
【问题描述】:

我正在运行这段代码,有时我得到一个错误,其他我没有,我真的不明白为什么。

let promiseLogIn = new Promise(function(reject,resolve){
  gapi.auth.authorize(authData , function(response) {
      var authButton = document.getElementById('auth-button');
      if (response.error) {
        console.log("AuthBad");
        resolve();
        authButton.hidden = false;
      }
      else {
        console.log("AuthGood");
        reject();
        authButton.hidden = true;
      }
    });
  });

我在我的 html 文件中加载 lib,如下所示:

<script src="https://apis.google.com/js/client.js?onload=authorize"></script>
<script src="sources/scripts/dist/bundle.js"></script>

其中 bundle.j 是我拥有所有已编译 ts 文件的位置。

【问题讨论】:

  • 你确定要在打字稿下标记这个吗?
  • 您确定您正确加载了 gapi 库吗?
  • @Aron 这是使用打字稿构建的,我应该如何正确加载它?
  • @Aron 在我的 html 中我有: 其中 bundle.js ths Ts 编译
  • 请也使用脚本标签编辑您的问题

标签: typescript google-api google-api-js-client


【解决方案1】:

您可能会收到此错误,因为您的范围内没有 auth2 库。要将库添加到您的 google 范围,Google 的示例文档鼓励使用 gapi.load() 方法加载新包。所以,你可以这样做:

let promiseLogIn = new Promise(function(reject,resolve){
  gapi.load('auth2', function(){
    gapi.auth.authorize(authData , function(response) {
      var authButton = document.getElementById('auth-button');
      if (response.error) {
        console.log("AuthBad");
        resolve();
        authButton.hidden = false;
      }
      else {
        console.log("AuthGood");
        reject();
        authButton.hidden = true;
      }
    });
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 2014-08-09
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多