谷歌api示例不起作用

时间:2013-03-21 18:20:01

标签: javascript google-api

我正在尝试运行以下Google Api示例:

<:html:>
  <:body:>
    <:div id='content':>
      <:h1:>Events<:/h1:>
      <:ul id='events':><:/ul:>
    <:/div:>
    <:a href='#' id='authorize-button' onclick='handleAuthClick();':>Login<:/a:>

    <:script:>

var clientId = redacted; 
var apiKey = redacted;
var scopes = 'https://www.googleapis.com/auth/calendar';

      function handleClientLoad() {
  gapi.client.setApiKey(apiKey);
  window.setTimeout(checkAuth,1);
  checkAuth();
}

function checkAuth() {
  gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true},
      handleAuthResult);
}

function handleAuthResult(authResult) {
  var authorizeButton = document.getElementById('authorize-button');
  if (authResult) {
    authorizeButton.style.visibility = 'hidden';
    makeApiCall();
  } else {
    authorizeButton.style.visibility = '';
    authorizeButton.onclick = handleAuthClick;
   }
}

function handleAuthClick(event) {
  gapi.auth.authorize(
      {client_id: clientId, scope: scopes, immediate: false},
      handleAuthResult);
  return false;
}
function makeApiCall() {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.list({
      'calendarId': 'primary'
    });

    request.execute(function(resp) {
      for (var i = 0; i <: resp.items.length; i++) {
        var li = document.createElement('li');
        li.appendChild(document.createTextNode(resp.items[i].summary));
        document.getElementById('events').appendChild(li);
      }
    });
  });
}
    <:/script:>
    <:script src="https://apis.google.com/js/client.js?onload=handleClientLoad":><:/script:>
  <:/body:>
<:/html:>

每当我运行文件时,我都会遇到两个错误:

gapi.client对象为null或未定义

window.sessionStorage.length对象为null或undefined

对于最后一个错误,它将此url作为源: https://apis.google.com//scs/apps-static//js/k=oz.gapi.nl.4xKjGS6fluU.O/m=client/am=QQ/rt=j/d=1/rs=AItRSTMdnq2AHV2okN-h3tZllkPQibG86w/cb=gapi.loaded_0

我正在运行IE8,有没有人知道出了什么问题?

2 个答案:

答案 0 :(得分:0)

function handleClientLoad() {
  gapi.client.setApiKey(apiKey);
  window.setTimeout(checkAuth,1);
  checkAuth();
}

您已拨打checkAuth()两次。也许setTimeout()对于有效的回复是必要的吗?

答案 1 :(得分:0)

我注意到官方文档页面here

中的相同示例(如hello world)

宣布使用变量:

ApiController

但如果使用浏览器API密钥,则developerKey变量会返回API无法访问的错误,并且在创建服务器密钥并使用它时,一切正常。