谷歌日历导出应用程序错误

Google calendar export application error

本文关键字:应用程序 错误 日历 谷歌      更新时间:2023-09-26

我正在编写一个简短的应用程序,用于将事件导出到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...

听起来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)