If you work with Google Calendars you know that you need to specify the ID of a calendar in order to be able to update it. A small modification to the code provided by Google will print all your calendar objects to the console and then you can find the specific calendar’s ID.
Be sure to read this over at Google: Authentication using the Google APIs Client Library for JavaScript. Specifically you need to generate the access keys for your application before you can call the calendar.calendarList.list method to see all the calendars in your account.
I won’t walk through the whole thing – see below and note the comments (As an aside this was relevant to something I did that inserted calendar entries into a Google calendar – see this repo).
var CLIENT_ID = 'your_client_id' var SCOPES = ['https://www.googleapis.com/auth/calendar']; // Check if current user has authorized this application. function checkAuth() { gapi.auth.authorize( { 'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true }, handleAuthResult); } //* Handle response from authorization server. //* @param {Object} authResult Authorization result. function handleAuthResult(authResult) { var authorizeDiv = document.getElementById('authorize-div'); if (authResult && !authResult.error) { // Hide auth UI, then load client library. authorizeDiv.style.display = 'none'; loadCalendarApi(); } else { // Show auth UI, allowing the user to initiate authorization by // clicking authorize button. authorizeDiv.style.display = 'inline'; } } //* Initiate auth flow in response to user clicking authorize button. //* @param {Event} event Button click event. function handleAuthClick(event) { gapi.auth.authorize( {client_id: CLIENT_ID, scope: SCOPES, immediate: false}, handleAuthResult); return false; } //* Load Google Calendar client library. List upcoming events //* once client library is loaded. function loadCalendarApi() { // Now get the list of calendars for the primary google account gapi.client.load('calendar', 'v3', function() { var request = gapi.client.calendar.calendarList.list({ 'calendarId': 'primary' }); request.execute(function(resp) { for (var i = 0; i < resp.items.length; i++) { console.log(resp.items[i]); } }); }); }
Also, place the following in your html:
<div id="authorize-div" style="display: "> <span>Authorize access to Google Calendar API <!--Button for the user to click to initiate auth sequence --> <button id="authorize-button" onclick="handleAuthClick(event)"> Authorize </button> </div> <pre id="output"></pre>