我正在编写一个用于将事件导出到 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...