Created
July 3, 2015 21:42
-
-
Save edprince/50cf0c6525d02e821a7d to your computer and use it in GitHub Desktop.
Displays events from google calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var apiKey = 'AIzaSyBnv4Qew7NmjUUW-7tJ21dBCV7l2-bN_Nc'; | |
var clientId = '837078947181-cua2g11v5hpbgfeaaq8buacdg935t2sj.apps.googleusercontent.com'; | |
var scopes = 'https://www.googleapis.com/auth/calendar'; | |
//Handles authorization | |
//function called when javascript client library loads | |
function handleClientLoad() { | |
gapi.client.setApiKey(apiKey); | |
window.setTimeOut(checkAuth,1); | |
} | |
function checkAuth() { | |
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, | |
handleAuthResult); | |
} | |
function handleAuthResult(authResult) { | |
var authorizeButton = document.getElementById('authorize-button'); | |
if (authResult && !authResult.error) { | |
authorizeButton.style.visibility = 'hidden'; | |
makeApiCall(); | |
} else { | |
authorizeButton.style.visibility = ''; | |
authorizeButton.onclick = handleAuthClick; | |
} | |
} | |
//called on button click, calls library authorize method | |
function handleAuthClick(event) { | |
gapi.auth.authorize( | |
{ | |
client_id: clientId, | |
scope: scopes, | |
immediate: false | |
}, | |
handleAuthResult); | |
return false; | |
} | |
//request data | |
function makeApiCall() { | |
gapi.client.load('calendar', 'v3', function() { | |
var request = gapi.client.calendar.events.list({ | |
'calendarId': 'primary' | |
}); | |
request.execute(function(resp) { | |
//function that calculates current date and only keeps values of | |
//Set the max number of items to display | |
var maxItems = resp.items.length; | |
//loop over all results, appending summary and date to the list element | |
console.log(resp); | |
for (var i = 0; i < maxItems; i++) { | |
var li = document.createElement('li'); | |
var eventSummary = (resp.items[i].summary); | |
var dateTime = (resp.items[i].start.dateTime); | |
console.log(typeof resp.items[i].start.dateTime); | |
var d = new Date(); | |
console.log(dateTime); | |
console.log(typeof dateTime); | |
console.log(calculateUnixTimestamp(dateTime), " ", calculateUnixTimestamp(d)); | |
if (calculateUnixTimestamp(dateTime) >= calculateUnixTimestamp(d)) { | |
console.log(dateTime); | |
console.log(Date.parse(dateTime)/1000); | |
var date = dateTime.split('T')[0]; | |
li.appendChild(document.createTextNode(eventSummary)); | |
console.log(date); | |
document.getElementById('events').appendChild(li); | |
var br = document.createElement('br'); | |
li.appendChild(br); | |
//var p = document.createElement('p'); | |
//li.appendChild(p); | |
//document.getElementById('time').innerText = date; | |
li.appendChild(document.createTextNode(date)); | |
li.setAttribute('id', 'list-data'); | |
document.getElementById('events').appendChild(li); | |
} | |
} | |
}); | |
}); | |
} | |
function calculateUnixTimestamp(x) { | |
return Date.parse(x)/1000; | |
} | |
window.addEventListener('load', function() { | |
var button = document.getElementById('authorize-button'); | |
button.addEventListener('click', handleAuthClick); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment