0

我正在编写一个用于将事件导出到 Google 日历的简短应用程序。(事件是从我网站的代码处理信息中获得的。)但是,当我单击按钮时,我编写的脚本给了我一个奇怪的错误。我第一次单击按钮时遇到的错误是:Uncaught TypeError: Cannot call method 'setApiKey' of undefined. 但是,第二次点击按钮没有刷新页面,错误消失,代码运行完美。

这是我的代码,你可以看到我在设置之前定义了 api 键:

var exportCalendarToGoogle = function() {
    var clientId = '38247913478902437.google@user...';
    var scope = 'https://www.googleapis.com/auth/calendar';
    var apiKey = 'JDKLSFDIOP109321403AJSL';

    var withGApi = function() {
        gapi.client.setApiKey(apiKey);
        gapi.auth.init(checkAuth);
      }
    var checkAuth = function() {
        gapi.auth.authorize({client_id: clientId, scope: scope, immediate: false}, handleAuthResult);
    }

    var handleAuthResult = function(authResult) {
        if(authResult) {
            gapi.client.load("calendar", "v3", exportCalendar);
        } else {
            alert("Authentication failed: please enter correct login information.");
        }
    }
 //functions to format the calendar json input to Google calendar...
4

1 回答 1

0

听起来好像gapi还没有完全加载

http://code.google.com/p/google-api-javascript-client/wiki/GettingStarted

有两个回调:

1) 在 URL 中加载 gapi 代码:

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

2) 您还可以提供一个回调函数,让您知道特定 API 何时加载:

gapi.client.load('plus', 'v1', function() { console.log('loaded.'); });

我认为你的问题是 1)

于 2013-01-28T22:44:45.903 回答