首页 > 解决方案 > gapi.auth2.getAuthInstance() 为 null 并且 gapi.client.init 永远不会返回错误

问题描述

我正在尝试让 google API 在我的代码中工作。我使用了快速入门示例,并使其与我的 API 密钥和客户端 ID 一起使用。但是,当我将完全相同的函数及其参数移到我的代码中时:

initClient: function () {
    gapi.client.init({
      apiKey: this.API_KEY,
      clientId: this.CLIENT_ID,
      discoveryDocs: this.DISCOVERY_DOCS,
      scope: this.SCOPES
    }).then(function () {
      // Listen for sign-in state changes.
      gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus);

      // Handle the initial sign-in state.
      //this.updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
      //this.authorizeButton.onclick = this.handleAuthClick;
      //this.signoutButton.onclick = this.handleSignoutClick;
    }, function(error) {
        alert(JSON.stringify(error, null, 2));
    });
},

我明白了Uncaught TypeError: Cannot read property 'isSignedIn' of null

此外,无论我对 gapi.client.init 参数做什么,函数(错误)都不会触发 - 删除它们或放置不正确的键 - 它永远不会显示警报。我使用 Chrome 开发工具来确保我不会运行旧的缓存代码或其他东西。有什么问题?

标签: javascriptgoogle-api

解决方案


推荐阅读