为什么我得到一个gapi.client未定义的错误?

时间:2014-05-06 01:14:04

标签: javascript google-api

我开始使用Google API。我有一个网络应用程序,我想连接到谷歌日历。我正在使用以下示例,只是为了解决问题:

<html>
  <head>
    <script>
      function appendResults(text) {
        console.log("***** DEBUG_jwir3: Appending results...");
        var results = document.getElementById('results');
        results.appendChild(document.createElement('P'));
        results.appendChild(document.createTextNode(text));
      }

      function makeRequest() {
        console.log("***** DEBUG_jwir3: Making request...");
        var request = gapi.client.urlshortener.url.get({
          'shortUrl': 'http://goo.gl/fbsS'
        });
        request.execute(function(response) {
          appendResults(response.longUrl);
        });
      }

      function load() {
        console.log("***** DEBUG_jwir3: Loading....");
        gapi.client.setApiKey('AIzaSyDkiyjtUnxlzQUyxnu0WqUtkhyV8Q2KyA0');
        gapi.client.load('urlshortener', 'v1', makeRequest);
      }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=load"></script>
  </head>
  <body>
    <div id="results"></div>
  </body>
</html>

这些代码大部分是直接从https://developers.google.com/api-client-library/javascript/samples/samples#LoadinganAPIandMakingaRequest获取的,但我稍微修改了一下,输出了一些有关正在发生的事情的数据。我得到以下内容:

GET https://apis.google.com/js/client.js [HTTP/1.1 200 OK 71ms]
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. test.html
GET https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en.pjG1qAV7lig.O/m=client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AItRSTOwlG0qQ6tB8ZQLSqOIeICvIpAiTw/cb=gapi.loaded_0 [HTTP/1.1 200 OK 657ms]
TypeError: gapi.client is undefined test.html:23
GET https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en.pjG1qAV7lig.O/m=debug_error/exm=client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AItRSTOwlG0qQ6tB8ZQLSqOIeICvIpAiTw/cb=gapi.loaded_1 [HTTP/1.1 200 OK 44ms]
"***** DEBUG_jwir3: Loading...." test.html:22
TypeError: c is undefined

我不太确定这里发生了什么。我在google开发者控制台中启用了urlshortener API。另外,我是从localhost机器运行的,所以我的理解是应该允许所有localhost或file:// referrer请求。我确实让我的API密钥允许所有引用,但使用通配符:*。

对我可能做错了什么的任何想法?

1 个答案:

答案 0 :(得分:1)

这可能是因为您的浏览器阻止了google.com上的Cookie。

我将Firefox永久设置为阻止Cookie,除非我特意将网站列入白名单。默认情况下,google.com被屏蔽后,我会收到一个空白的Google日历,其中包含您报告的错误,但是日历显示的是google.com允许使用Cookie。

相关问题