Google Calendar Samples · KcsDev1982/sunbelt-plb-samples@b06f366 · GitHub
Skip to content

Commit b06f366

Browse files
committed
Google Calendar Samples
Updates for 10.8 includes and Google Calendar samples
1 parent dde89e5 commit b06f366

18 files changed

Lines changed: 1861 additions & 11 deletions

README.md

Lines changed: 1 addition & 0 deletions

calendar/gcal_logon.html

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<!--
7+
This code is taken from the sample given at https://developers.google.com/workspace/calendar/api/quickstart/js
8+
-->
9+
<div id='linkStore' style="display: none"></div>
10+
<script type="text/javascript">
11+
12+
var CLIENT_ID;
13+
var API_KEY;
14+
15+
// Discovery doc URL for APIs used by the quickstart
16+
const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest';
17+
18+
// Authorization scopes required by the API; multiple scopes can be
19+
// included, separated by spaces.
20+
//const SCOPES = 'https://www.googleapis.com/auth/calendar.readonly';
21+
const SCOPES = 'https://www.googleapis.com/auth/calendar';
22+
23+
let tokenClient;
24+
let gapiNowLoaded = false;
25+
let gisNowLoaded = false;
26+
let gapiInited = false;
27+
let gisInited = false;
28+
29+
/**
30+
* Callback after api.js is loaded.
31+
*/
32+
function gapiLoaded() {
33+
gapiNowLoaded = true;
34+
}
35+
36+
/**
37+
* Callback after the API client is loaded. Loads the
38+
* discovery doc to initialize the API.
39+
*/
40+
async function initializeGapiClient() {
41+
await gapi.client.init({
42+
apiKey: API_KEY,
43+
discoveryDocs: [DISCOVERY_DOC],
44+
});
45+
gapiInited = true;
46+
gapiNowLoaded = false;
47+
48+
}
49+
50+
/**
51+
* Callback after Google Identity Services are loaded.
52+
*/
53+
function gisLoaded() {
54+
55+
gisNowLoaded = true;
56+
57+
}
58+
59+
function gisLoadedFini() {
60+
tokenClient = google.accounts.oauth2.initTokenClient({
61+
client_id: CLIENT_ID,
62+
scope: SCOPES,
63+
callback: '', // defined later
64+
});
65+
gisInited = true;
66+
gisNowLoaded = false;
67+
68+
}
69+
70+
/**
71+
* Sign in the user
72+
*/
73+
function doAuth() {
74+
75+
tokenClient.callback = async (resp) => {
76+
if (resp.error !== undefined) {
77+
throw (resp);
78+
}
79+
80+
};
81+
82+
if (gapi.client.getToken() === null) {
83+
// Prompt the user to select a Google Account and ask for consent to share their data
84+
// when establishing a new session.
85+
tokenClient.requestAccessToken({prompt: 'consent'});
86+
} else {
87+
// Skip display of account chooser and consent dialog for an existing session.
88+
tokenClient.requestAccessToken({prompt: ''});
89+
}
90+
}
91+
92+
/**
93+
* Sign out the user
94+
*/
95+
function doSignout() {
96+
const token = gapi.client.getToken();
97+
if (token !== null) {
98+
google.accounts.oauth2.revoke(token.access_token);
99+
gapi.client.setToken('');
100+
document.getElementById('content').innerText = '';
101+
}
102+
}
103+
104+
/**
105+
* Add and event
106+
*/
107+
function addEvent( newEvent, calId ) {
108+
const request = gapi.client.calendar.events.insert({
109+
'calendarId': calId, 'resource': newEvent });
110+
111+
request.execute(function(event) {
112+
/* document.getElementById('linkStore').innerText = event.htmlLink; */ });
113+
}
114+
115+
/**
116+
* Handle message requests
117+
*/
118+
window.addEventListener('message', (event) => {
119+
var rMsg;
120+
const msgData = event.data.slice(0,10);
121+
if ( msgData == '{"plbmsg":' ) {
122+
try {
123+
rMsg = JSON.parse(event.data);
124+
}
125+
catch (exception) {
126+
console.log(exception);
127+
return;
128+
}
129+
130+
switch (rMsg.plbmsg) {
131+
case 0:
132+
CLIENT_ID = rMsg.arg1;
133+
API_KEY = rMsg.arg2;
134+
if ( gapiNowLoaded ) {
135+
gapi.load('client', initializeGapiClient);
136+
}
137+
if ( gisNowLoaded ) {
138+
gisLoadedFini();
139+
}
140+
break;
141+
142+
case 1:
143+
if (gapiInited && gisInited) {
144+
doAuth();
145+
}
146+
break;
147+
case 2: doSignout(); break;
148+
case 3: addEvent( rMsg.arg1, rMsg.arg2 ); break;
149+
}
150+
}
151+
});
152+
153+
</script>
154+
<script async defer src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
155+
<script async defer src="https://accounts.google.com/gsi/client" onload="gisLoaded()"></script>
156+
</body>
157+
</html>

calendar/gcalendar.plf

4.12 KB
Binary file not shown.

calendar/gcalendar.pls

Lines changed: 194 additions & 0 deletions

0 commit comments

Comments
 (0)