GoogleCalendarAPIのQuickstartsを参考にしたのですが、このサンプルのままだと認証ボタンを押して、Googleのログインを行わないとカレンダーが表示されません。
また、appendしたhtmlがそのまま文字で表示されてしまいます。
- Googleのログインなしに、ページロード時に表示させる方法
- appendしたhtmlの訂正方法
の2点について教えてください。
html
1<!-- Quickstarts参照 --> 2<!DOCTYPE html> 3<html> 4 <head> 5 <title>Google Calendar API Quickstart</title> 6 <meta charset="utf-8" /> 7 </head> 8 <body> 9 <p>Google Calendar API Quickstart</p> 10 11 <!--Add buttons to initiate auth sequence and sign out--> 12 <button id="authorize_button" style="display: none;">Authorize</button> 13 <button id="signout_button" style="display: none;">Sign Out</button> 14 15 <pre id="content" style="white-space: pre-wrap;"></pre> 16 17 <script type="text/javascript"> 18 // Client ID and API key from the Developer Console 19 var CLIENT_ID = 'クライアントID'; 20 var API_KEY ='APIKey'; 21 // Array of API discovery doc URLs for APIs used by the quickstart 22 var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"]; 23 // Authorization scopes required by the API; multiple scopes can be 24 // included, separated by spaces. 25 var SCOPES = "https://www.googleapis.com/auth/calendar.readonly"; 26 var authorizeButton = document.getElementById('authorize_button'); 27 var signoutButton = document.getElementById('signout_button'); 28 /** 29 * On load, called to load the auth2 library and API client library. 30 */ 31 function handleClientLoad() { 32 gapi.load('client:auth2', initClient); 33 } 34 /** 35 * Initializes the API client library and sets up sign-in state 36 * listeners. 37 */ 38 function initClient() { 39 gapi.client.init({ 40 apiKey: API_KEY, 41 clientId: CLIENT_ID, 42 discoveryDocs: DISCOVERY_DOCS, 43 scope: SCOPES 44 }).then(function () { 45 // Listen for sign-in state changes. 46 gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); 47 // Handle the initial sign-in state. 48 updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); 49 authorizeButton.onclick = handleAuthClick; 50 signoutButton.onclick = handleSignoutClick; 51 }, function(error) { 52 appendPre(JSON.stringify(error, null, 2)); 53 }); 54 } 55 /** 56 * Called when the signed in status changes, to update the UI 57 * appropriately. After a sign-in, the API is called. 58 */ 59 function updateSigninStatus(isSignedIn) { 60 if (isSignedIn) { 61 authorizeButton.style.display = 'none'; 62 signoutButton.style.display = 'block'; 63 listUpcomingEvents(); 64 } else { 65 authorizeButton.style.display = 'block'; 66 signoutButton.style.display = 'none'; 67 } 68 } 69 /** 70 * Sign in the user upon button click. 71 */ 72 function handleAuthClick(event) { 73 gapi.auth2.getAuthInstance().signIn(); 74 } 75 /** 76 * Sign out the user upon button click. 77 */ 78 function handleSignoutClick(event) { 79 gapi.auth2.getAuthInstance().signOut(); 80 } 81 /** 82 * Append a pre element to the body containing the given message 83 * as its text node. Used to display the results of the API call. 84 * 85 * @param {string} message Text to be placed in pre element. 86 */ 87 function appendPre(message) { 88 var pre = document.getElementById('content'); 89 var textContent = document.createTextNode(message + '\n'); 90 pre.appendChild(textContent); 91 } 92 /** 93 * Print the summary and start datetime/date of the next ten events in 94 * the authorized user's calendar. If no events are found an 95 * appropriate message is printed. 96 */ 97 function listUpcomingEvents() { 98 gapi.client.calendar.events.list({ 99 'calendarId': 'カレンダーID', 100 //'timeMin': (new Date()).toISOString(), 101 'showDeleted': false, 102 'singleEvents': true, 103 'maxResults': 10, 104 'orderBy': 'startTime' 105 }).then(function(response) { 106 var events = response.result.items; 107 appendPre('Upcoming events:'); 108 if (events.length > 0) { 109 for (i = 0; i < events.length; i++) { 110 var event = events[i]; 111 var when = event.start.dateTime; 112 if (!when) { 113 when = event.start.date; 114 } 115 appendPre('<article><h1>'+event.summary+'</h1><p>' + when + '</p></article>') 116 } 117 } else { 118 appendPre('No upcoming events found.'); 119 } 120 }); 121 } 122 </script> 123 124 <script async defer src="https://apis.google.com/js/api.js" 125 onload="this.onload=function(){};handleClientLoad()" 126 onreadystatechange="if (this.readyState === 'complete') this.onload()"> 127 </script> 128 </body> 129</html>
回答1件
あなたの回答
tips
プレビュー