content/hifi-content/brosche/dev/googleCalendar/test.html
2022-02-13 21:50:01 +01:00

152 lines
No EOL
5.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Google OAuth API Quickstart</title>
<meta charset="utf-8" />
</head>
<body>
<p>Google OAuth API Quickstart</p>
<pre id="content" style="white-space: pre-wrap;"></pre>
<script type="text/javascript">
// Load the API's client and auth2 modules.
// Call the initClient function after the modules load.
var API_KEY = 'AIzaSyCu8nILlmjRhMMwtIKkV15BBrks-QXjFtk';
var CLIENT_ID = '813544734011-7b3uig9o47b15bmh7bbc2jq93k291lkf.apps.googleusercontent.com';
var CLIENT_SECRET = 'WFEX3xCgxkUlNjiwt6WjJaKY';
var SCOPE = 'https://www.googleapis.com/auth/calendar.events.readonly';
var authCode = false;
var GoogleAuth;
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function initClient() {
var discoveryUrl = 'https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest';
gapi.client.init({
'apiKey': API_KEY,
'discoveryDocs': [discoveryUrl],
'clientId': CLIENT_ID,
'scope': SCOPE
}).then(function () {
GoogleAuth = gapi.auth2.getAuthInstance();
// Listen for sign-in state changes.
GoogleAuth.isSignedIn.listen(updateSigninStatus);
GoogleAuth.grantOfflineAccess().then(function (response) {
authCode = response.code;
var zone = new Date().toLocaleTimeString('en-us',{timeZoneName:'short'}).split(' ')[2];
EventBridge.emitWebEvent(JSON.stringify({
type: "AUTHCODE",
authCode: authCode,
clientID: CLIENT_ID,
secret: CLIENT_SECRET,
timezone: zone
}));
});
// Handle initial sign-in state. (Determine if user is already signed in.)
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
var user = GoogleAuth.currentUser.get();
setSigninStatus();
// Call handleAuthClick function when user clicks on
// "Sign In/Authorize" button.
$('#sign-in-or-out-button').click(function() {
handleAuthClick();
});
$('#revoke-access-button').click(function() {
revokeAccess();
});
});
}
function handleAuthClick() {
if (GoogleAuth.isSignedIn.get()) {
// User is authorized and has clicked 'Sign out' button.
GoogleAuth.signOut();
} else {
// User is not signed in. Start Google auth flow.
GoogleAuth.signIn();
}
}
function revokeAccess() {
GoogleAuth.disconnect();
}
function setSigninStatus(isSignedIn) {
var user = GoogleAuth.currentUser.get();
var isAuthorized = user.hasGrantedScopes(SCOPE);
if (isAuthorized) {
$('#sign-in-or-out-button').html('Sign out');
$('#revoke-access-button').css('display', 'inline-block');
$('#auth-status').html('You are currently signed in and have granted ' +
'access to this app.');
EventBridge.emitWebEvent(JSON.stringify({
type: "TOKEN",
token: response.access_token,
expires_at: response.expires_at,
expires_in: response.expires_in,
clientID: CLIENT_ID,
secret: CLIENT_SECRET,
timezone: zone
}));
} else {
$('#sign-in-or-out-button').html('Sign In/Authorize');
$('#revoke-access-button').css('display', 'none');
$('#auth-status').html('You have not authorized this app or you are ' +
'signed out.');
}
}
function updateSigninStatus(isSignedIn) {
setSigninStatus();
}
// This function receives and attempts to parse data from
// High Fidelity scripts.
function onScriptEventReceived(data) {
var data = JSON.parse(data);
switch (data.type) {
case "RENEW_TOKEN":
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
break;
}
}
// This function sets up the communication channel between
// this HTML and High Fidelity scripts that message it.
var EVENT_BRIDGE_SETUP_DELAY = 100;
function onLoad() {
setTimeout(() => {
EventBridge.scriptEventReceived.connect(onScriptEventReceived);
EventBridge.emitWebEvent(JSON.stringify({
type: "EVENT_BRIDGE_OPEN_MESSAGE"
}));
}, EVENT_BRIDGE_SETUP_DELAY);
}
onLoad();
</script>
<button id="sign-in-or-out-button"
style="margin-left: 25px">Sign In/Authorize</button>
<button id="revoke-access-button"
style="display: none; margin-left: 25px">Revoke access</button>
<div id="auth-status" style="display: inline; padding-left: 25px"></div><hr>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></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>