2017-05-27 102 views
0

我正在嘗試使用Google Calendar API嘗試爲用戶設置提醒。我認爲設置日曆活動並設置提醒可以做我需要的。使用Calendar API創建的事件未顯示在日曆中

我正在使用此示例代碼從谷歌嘗試的API,我回來了我創建的事件的描述。但是,如果我登錄到我的Google日曆,我無法在那裏看到活動。

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <title>Google Calendar API Quickstart</title> 
 
    <meta charset='utf-8' /> 
 
    </head> 
 
    <body> 
 
    <p>Google Calendar API Quickstart</p> 
 

 
    <!--Add buttons to initiate auth sequence and sign out--> 
 
    <button id="authorize-button" style="display: none;">Authorize</button> 
 
    <button id="signout-button" style="display: none;">Sign Out</button> 
 

 
    <pre id="content"></pre> 
 

 
    <script type="text/javascript"> 
 
     // Client ID and API key from the Developer Console 
 
     var CLIENT_ID ='781531190408-u3gh34li8b0um74r11m8hsbcofbpj4jf.apps.googleusercontent.com'; 
 

 
     // Array of API discovery doc URLs for APIs used by the quickstart 
 
     var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"]; 
 

 
     // Authorization scopes required by the API; multiple scopes can be 
 
     // included, separated by spaces. 
 
     var SCOPES = "https://www.googleapis.com/auth/calendar.readonly"; 
 

 
     var authorizeButton = document.getElementById('authorize-button'); 
 
     var signoutButton = document.getElementById('signout-button'); 
 

 
     /** 
 
     * On load, called to load the auth2 library and API client library. 
 
     */ 
 
     function handleClientLoad() { 
 
     gapi.load('client:auth2', initClient); 
 
     } 
 

 
     /** 
 
     * Initializes the API client library and sets up sign-in state 
 
     * listeners. 
 
     */ 
 
     function initClient() { 
 
     gapi.client.init({ 
 
      discoveryDocs: DISCOVERY_DOCS, 
 
      clientId: CLIENT_ID, 
 
      scope: SCOPES 
 
     }).then(function() { 
 
      // Listen for sign-in state changes. 
 
      gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); 
 

 
      // Handle the initial sign-in state. 
 
      updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); 
 
      authorizeButton.onclick = handleAuthClick; 
 
      signoutButton.onclick = handleSignoutClick; 
 
     }); 
 
     } 
 

 
     /** 
 
     * Called when the signed in status changes, to update the UI 
 
     * appropriately. After a sign-in, the API is called. 
 
     */ 
 
     function updateSigninStatus(isSignedIn) { 
 
     if (isSignedIn) { 
 
      authorizeButton.style.display = 'none'; 
 
      signoutButton.style.display = 'block'; 
 
      listUpcomingEvents(); 
 
      // Refer to the JavaScript quickstart on how to setup the environment: 
 
// https://developers.google.com/google-apps/calendar/quickstart/js 
 
// Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any 
 
// stored credentials. 
 

 
var event = /*{ 
 
    'summary': 'Google I/O 2015', 
 
    'location': '800 Howard St., San Francisco, CA 94103', 
 
    'description': 'A chance to hear more about Google\'s developer products.', 
 
    'start': { 
 
    'dateTime': '2017-07-28T09:00:00-07:00', 
 
    'timeZone': 'America/Los_Angeles' 
 
    }, 
 
    'end': { 
 
    'dateTime': '2017-07-28T17:00:00-07:00', 
 
    'timeZone': 'America/Los_Angeles' 
 
    }, 
 
    'recurrence': [ 
 
    'RRULE:FREQ=DAILY;COUNT=2' 
 
    ], 
 
    'attendees': [ 
 
    {'email': '[email protected]'}, 
 
    {'email': '[email protected]'} 
 
    ], 
 
    'reminders': { 
 
    'useDefault': false, 
 
    'overrides': [ 
 
     {'method': 'email', 'minutes': 24 * 60}, 
 
     {'method': 'popup', 'minutes': 10} 
 
    ] 
 
    } 
 
};*/{ 
 
    "reminders": { 
 
    "useDefault": false, 
 
    "overrides": [ 
 
     { 
 
     "method": "email", 
 
     "minutes": 15 
 
     }, 
 
     { 
 
     "method": "popup", 
 
     "minutes": 15 
 
     }, 
 
     { 
 
     "method": "popup", 
 
     "minutes": 5 
 
     } 
 
    ] 
 
    }, 
 
    "start": { 
 
    "dateTime": "2017-07-12T10:30:00.0z" 
 
    }, 
 
    "end": { 
 
    "dateTime": "2017-07-12T11:30:00.0z" 
 
    }, 
 
    "description": "Just a test description " 
 
}; 
 

 
var request = gapi.client.calendar.events.insert({ 
 

 
    'calendarId': 'primary', 
 
    'resource': event 
 
}); 
 

 
request.execute(function(event) { 
 
    appendPre('Event created: ' + event.description); 
 
}); 
 
     } else { 
 
      authorizeButton.style.display = 'block'; 
 
      signoutButton.style.display = 'none'; 
 
     } 
 
     } 
 

 
     /** 
 
     * Sign in the user upon button click. 
 
     */ 
 
     function handleAuthClick(event) { 
 
     gapi.auth2.getAuthInstance().signIn(); 
 
     } 
 

 
     /** 
 
     * Sign out the user upon button click. 
 
     */ 
 
     function handleSignoutClick(event) { 
 
     gapi.auth2.getAuthInstance().signOut(); 
 
     } 
 

 
     /** 
 
     * Append a pre element to the body containing the given message 
 
     * as its text node. Used to display the results of the API call. 
 
     * 
 
     * @param {string} message Text to be placed in pre element. 
 
     */ 
 
     function appendPre(message) { 
 
     var pre = document.getElementById('content'); 
 
     var textContent = document.createTextNode(message + '\n'); 
 
     pre.appendChild(textContent); 
 
     } 
 

 
     /** 
 
     * Print the summary and start datetime/date of the next ten events in 
 
     * the authorized user's calendar. If no events are found an 
 
     * appropriate message is printed. 
 
     */ 
 
     function listUpcomingEvents() { 
 
     gapi.client.calendar.events.list({ 
 
      'calendarId': 'primary', 
 
      'timeMin': (new Date()).toISOString(), 
 
      'showDeleted': false, 
 
      'singleEvents': true, 
 
      'maxResults': 10, 
 
      'orderBy': 'startTime' 
 
     }).then(function(response) { 
 
      var events = response.result.items; 
 
      appendPre('Upcoming events:'); 
 

 
      if (events.length > 0) { 
 
      for (i = 0; i < events.length; i++) { 
 
       var event = events[i]; 
 
       var when = event.start.dateTime; 
 
       if (!when) { 
 
       when = event.start.date; 
 
       } 
 
       appendPre(event.description + ' (' + when + ')') 
 
      } 
 
      } else { 
 
      appendPre('No upcoming events found.'); 
 
      } 
 
     }); 
 
     } 
 

 
    </script> 
 

 
    <script async defer src="https://apis.google.com/js/api.js" 
 
     onload="this.onload=function(){};handleClientLoad()" 
 
     onreadystatechange="if (this.readyState === 'complete') this.onload()"> 
 
    </script> 
 
    </body> 
 
</html>

難道我做錯了什麼?謝謝。

回答

0

我試過了JavaScript Quickstart並使用這個sample code插入了一個事件,我得到了一個成功的迴應。也許問題是你如何處理你的授權。你可以檢查這個tutorial

一些額外的引用:

+0

我認爲該事件被創建,因爲拉即將發生的事件的其他功能拉,我剛剛創建的事件。問題是我不能在我的日曆上看到這個事件。我不應該像人們的生日那樣在日曆上看到它嗎? – PeeBee

相關問題