Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Google Calendar API Quickstart</title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <p>Google Calendar API Quickstart</p>
  9.  
  10. <!--Add buttons to initiate auth sequence and sign out-->
  11. <button id="authorize_button" style="display: none;">Authorize</button>
  12. <button id="signout_button" style="display: none;">Sign Out</button>
  13.  
  14. <pre id="content" style="white-space: pre-wrap;"></pre>
  15.  
  16. <script type="text/javascript">
  17. // Client ID and API key from the Developer Console
  18. var CLIENT_ID = '576810460119-0iiqdnglmprmdvps96l2gi2tri7mm6h5.apps.googleusercontent.com';
  19. var API_KEY = 'AIzaSyCbRptKwLIHSNK6SnZWHErcxX5e8-fZl3g';
  20.  
  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.  
  24. // Authorization scopes required by the API; multiple scopes can be
  25. // included, separated by spaces.
  26. var SCOPES = "https://www.googleapis.com/auth/calendar.readonly";
  27.  
  28. var authorizeButton = document.getElementById('authorize_button');
  29. var signoutButton = document.getElementById('signout_button');
  30.  
  31. /**
  32. * On load, called to load the auth2 library and API client library.
  33. */
  34. function handleClientLoad() {
  35. gapi.load('client:auth2', initClient);
  36. }
  37.  
  38. /**
  39. * Initializes the API client library and sets up sign-in state
  40. * listeners.
  41. */
  42. function initClient() {
  43. gapi.client.init({
  44. apiKey: API_KEY,
  45. clientId: CLIENT_ID,
  46. discoveryDocs: DISCOVERY_DOCS,
  47. scope: SCOPES
  48. }).then(function () {
  49. // Listen for sign-in state changes.
  50. gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
  51.  
  52. // Handle the initial sign-in state.
  53. updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
  54. authorizeButton.onclick = handleAuthClick;
  55. signoutButton.onclick = handleSignoutClick;
  56. }, function(error) {
  57. appendPre(JSON.stringify(error, null, 2));
  58. });
  59. }
  60.  
  61. /**
  62. * Called when the signed in status changes, to update the UI
  63. * appropriately. After a sign-in, the API is called.
  64. */
  65. function updateSigninStatus(isSignedIn) {
  66. if (isSignedIn) {
  67. authorizeButton.style.display = 'none';
  68. signoutButton.style.display = 'block';
  69. listUpcomingEvents();
  70. } else {
  71. authorizeButton.style.display = 'block';
  72. signoutButton.style.display = 'none';
  73. }
  74. }
  75.  
  76. /**
  77. * Sign in the user upon button click.
  78. */
  79. function handleAuthClick(event) {
  80. gapi.auth2.getAuthInstance().signIn();
  81. }
  82.  
  83. /**
  84. * Sign out the user upon button click.
  85. */
  86. function handleSignoutClick(event) {
  87. gapi.auth2.getAuthInstance().signOut();
  88. }
  89.  
  90. /**
  91. * Append a pre element to the body containing the given message
  92. * as its text node. Used to display the results of the API call.
  93. *
  94. * @param {string} message Text to be placed in pre element.
  95. */
  96. function appendPre(message) {
  97. var pre = document.getElementById('content');
  98. var textContent = document.createTextNode(message + '\n');
  99. pre.appendChild(textContent);
  100. }
  101.  
  102. /**
  103. * Print the summary and start datetime/date of the next ten events in
  104. * the authorized user's calendar. If no events are found an
  105. * appropriate message is printed.
  106. */
  107. function listUpcomingEvents() {
  108. gapi.client.calendar.events.list({
  109. 'calendarId': 'primary',
  110. 'timeMin': (new Date()).toISOString(),
  111. 'showDeleted': false,
  112. 'singleEvents': true,
  113. 'maxResults': 10,
  114. 'orderBy': 'startTime'
  115. }).then(function(response) {
  116. var events = response.result.items;
  117. appendPre('Upcoming events:');
  118.  
  119. if (events.length > 0) {
  120. for (i = 0; i < events.length; i++) {
  121. var event = events[i];
  122. var when = event.start.dateTime;
  123. if (!when) {
  124. when = event.start.date;
  125. }
  126. appendPre(event.summary + ' (' + when + ')')
  127. }
  128. } else {
  129. appendPre('No upcoming events found.');
  130. }
  131. });
  132. }
  133.  
  134. </script>
  135.  
  136. <script async defer src="https://apis.google.com/js/api.js"
  137. onload="this.onload=function(){};handleClientLoad()"
  138. onreadystatechange="if (this.readyState === 'complete') this.onload()">
  139. </script>
  140. </body>
  141. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement