イベントをGoogleカレンダーにエクスポートするための短いアプリケーションを書いています。(イベントは私のWebサイトのコード処理情報から取得されます。)しかし、ボタンをクリックすると、作成したスクリプトで奇妙なエラーが発生します。ボタンを初めてクリックしたときに発生するエラーは次のとおりUncaught TypeError: Cannot call method 'setApiKey' of undefined
です。ただし、ページを更新せずに2回目にボタンをクリックすると、エラーが消え、コードは完全に実行されます。
これが私のコードです。ご覧のとおり、設定する前に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...