0

我正在尝试运行以下 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 或未定义

对于最后一个错误,它将此网址作为来源: 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,有人知道出了什么问题吗?

4

2 回答 2

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

你打checkAuth()了两次电话。也许这setTimeout()是有效响应所必需的?

于 2013-06-19T11:34:25.130 回答
0

我注意到来自官方文档页面的相同示例(如 hello world)here

声明使用变量:

  // The Browser API key obtained from the Google Developers Console.
  var developerKey = 'xxxxxxxYYYYYYYY-12345678';

  // The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
  var clientId = "1234567890-abcdefghijklmnopqrstuvwxyz.apps.googleusercontent.com"

  // Scope to use to access user's photos.
  var scope = ['https://www.googleapis.com/auth/photos'];

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

于 2016-06-10T10:58:28.113 回答