After all the work I put into creating a server-side script to add events in Google Calendar, it turns out that due to security reasons I have to go the AJAX route. I should have gone with my gut feeling to do so in the beginning. :/
I created a javascript object for my calendar interactions. The javascript API docs are all goofed up (for instance, I find no direct link from the Data API Guide page for the current 2.0 version of the javascript docs – I had to do a separate google search) so a lot of this was trial and error. Hopefully it’s helpful, since I couldn’t find a solid example in the google-sphere.
The app completes the transaction with Google Calendar, then saves data to a local database. Unfortunately, I’m still not in the habit of commenting my code.
Login and Logout
//Login needs to happen before transactions,
//usually via a login button on the web page
function logMeIn() {
scope = "http://www.google.com/calendar/feeds";
var token = google.accounts.user.login(scope);
}
function logMeOut() {
scope = "http://www.google.com/calendar/feeds";
if (google.accounts.user.checkLogin(scope)) {
google.accounts.user.logout();
}
}
Javascript “oCal” object
//Google Calendar object constructor
var oCal = function(type, entryId) {
var myService;
var updater = {};
var eid = entryId; //global variable defined previously
var feedUrl = 'http://www.google.com/calendar/feeds/default/private/full';
function setupMyService() {
myService = new google.gdata.calendar.CalendarService('exampleApp-exampleApp-1');
}
//startdate and enddate need to be formatted in ISO = "2007-09-23T18:00:00.000Z"
function createEvent(startdate, enddate, subject, summary, name, address) {
var entry = new google.gdata.calendar.CalendarEventEntry({
title: {
type: 'text',
text: subject
},
content: {
type: 'text',
text: summary
},
locations: [{
rel: 'g.event',
label: 'Event Location',
valueString: name+', ' + address
}],
times: [{
startTime: google.gdata.DateTime.fromIso8601(startdate),
endTime: google.gdata.DateTime.fromIso8601(enddate)
}]
});
return entry;
}
var callback = function(result) {
alerter('Calendar updated, now saving event');
saveEvent(result.entry.getSelfLink().getHref());
}
function handleEntry(result) {
var entry = result.entry;
entry.getTitle().setText(updater.title);
entry.getContent().setText(updater.content);
entry.getLocations()[0].setValueString(updater.location);
entry.getTimes()[0].setStartTime(google.gdata.DateTime.fromIso8601(updater.sdate));
entry.getTimes()[0].setEndTime(google.gdata.DateTime.fromIso8601(updater.edate));
entry.updateEntry(handleUpdateEntry, handleError);
}
function handleDelete(result) {
result.entry.deleteEntry(handleDeletedEntry, handleError);
}
function handleUpdateEntry(result) {
alerter(result.entry.getTitle().getText() + ' saved');
saveEvent(eid);
}
function handleDeletedEntry(result) {
alerter('Calendar entry deleted');
}
function handleError(e) {
alert('Error: ' + (e.cause ? e.cause.statusText : e.message));
saveEvent(eid);
}
this.addEvent = function(startdate, enddate, subject, summary, name, address) {
var newEntry = createEvent(startdate, enddate, subject, summary, name, address);
setupMyService('add');
myService.insertEntry(feedUrl, newEntry, callback, handleError, google.gdata.calendar.CalendarEventEntry);
}
this.updateEvent = function(startdate, enddate, subject, summary, name, address) {
updater = {
sdate: startdate,
edate: enddate,
title: subject,
content: summary,
location: name + ', ' + address
}
setupMyService();
myService.getCalendarEventEntry(eid, handleEntry, handleError);
}
this.deleteEvent = function() {
setupMyService();
myService.getCalendarEventEntry(eid, handleDelete, handleError);
}
}
Hi, thanks for the code. Do you have the sampe code on calling the javascript function e.g. the step by step and possibly the sample interface. Thanks
July 21, 2010 @ 4:58 pm