Google Calendar is a lovely looking and acting "free online shareable calendar service", allowing you to maintain your personal schedule, share it with others, and view public calendars.
Google has also provided an API such that your programs and websites can make use of it remotely; for instance to view, add or delete events. There are special client libraries to let you use it from the Java or C# .NET programming languages. It also supports communication via standard XML, to allow any other suitable language to get involved.
The Poorhouse decided to go down the Visual Studio 6 route; particularly Visual Basic 6. There aren't many, if any, examples around of how to use the Google Calendar API with VB6, but below is an illustration of how it can be done relatively simply with Visual Basic. This example shows how to add an event to a calendar from a VB program local to your computer. In that it communicates via pure XML, most of this could easily be abstracted any other similiarish language.
The transaction with Google happen via HTTP POST requests. There is a control available in VS6 that allows you to perform these simply; the Microsoft Internet Transfer Control. It is basically a wrapper around the WinInet Windows API, making it nice and easy to use. To add this to your project, start a new standard .exe project in VB6. Go to the Project menu, then Components and pick the "Microsoft Internet Transfer Control". Click OK, and a component will be added to the components toolbox ready for use. Drop one of these onto your form.
Full instructions as to how to use the control are available from Microsoft. For now though, as we are doing POST requests we will need the .execute method of the Internet Transfer control.
To add an event to Google Calendar, we need to know that the calendar has been set up (Go here to set one up if you don't have one), and that you have your Google account's username (i.e. your email address) and password.
As per the API instructions you need to send a HTTP POST request to https://www.google.com/accounts/ClientLogin
This must include the following parameters.
- Email: your email address
- Passwd: your password
- source: an identifier for your application in the form companyName-applicationName-versionID
- service: what Google service you want to use; for the calendar this should always be cl.
In Visual Basic, you can do it as below this. Imagine Inet1 is the name of the Internet Transfer Control you added above, and your email address, password and application ID are stored in the string variables "myEmail", "myPassword" and "mySource". Note the use of the form-urlencoded Content-Type header.
strURL = "https://www.google.com/accounts/ClientLogin"
strFormData = "Email=" & myEmail & "&Passwd=" & myPassword & "&source=" & mySource & "&service=cl"
strHeaders = "Content-Type:application/x-www-form-urlencoded"
Inet1.Execute strURL, "POST", strFormData, strHeadersYour application needs to wait for Google to respond. Do this by looking out for the icResponseCompleted event in the Inet1_StateChanged event handler. When it does, store Google's reply in a variable, e.g.:
vtData = Inet1.GetChunk(1024, icString)
Do While LenB(vtData) > 0
outputString = outputString + vtData
' Get next chunk.
vtData = Inet1.GetChunk(1024, icString)
Loop
Response = outputStringIn that example, the Response variable now holds Google's response. What you need here, is the authorisation token given out in that response. You will then use that to prove that you are allowed to add an event to your calendar.
Therefore you should look for the phrase "Auth" in the response (outputString), and store the value for later. You can do it via:
AuthCode = Right(Response, Len(Response) - InStrRev(Response, "Auth=") - 4)The AuthCode variable will now hold Google's Authorisation token.
You now need to construct an event in XML. Check the Event Kind documentation from Google, or see the example on the Google API page. If you're doing it a lot, you'd probably want to use a XML tool to help you out, but as a one off you can just assemble it as a string. Make a string variable EventCode and assign the Event XML to it.
Now you must do a POST request to Google again; this time use the address http://www.google.com/calendar/feeds/default/private/full
Send your EventCode variable (the XML event) as the form data, and use the Content-Type and Authorization header. This should look like:
Content-Type:application/atom+xml
Authorization: GoogleLogin auth=[your authorization code]In VB, using the variables defined so far, you can do this by;
strURL = "http://www.google.com/calendar/feeds/default/private/full"
strFormData = EventCode
strHeaders = "Authorization: GoogleLogin auth=" & AuthCode & _
"Content-Type:application/atom+xml"
Inet1.Execute strURL, "POST", strFormData, strHeadersIf you check the response to your request, you should find Google sends you your data back, plus some ID values, with the header "201 Created". Job done. If you go and view your calendar now you should see the event.
As a proof of concept, a VB program is attached below that allows you to add an event to your Google calendar by filling the appropriate values. Be sure to keep the date and time fields in the same format as they default to.

The full source code is available in the text attachment to this post. The only fancy bit is the use of Karl Peterson's lovely CTimeZone VB class. This was used to make sure the Google event that this program made matched the timezone of the local computer. By default, Google Calendar creates events based on the Greenwich Mean Time timezone.
Note: Using the above procedure, I found that every time I restarted the program, the second step didn't work, and gave a 401 authorization required error when I tried to enter an event. Subsequent times worked well though. An explanation for this and a solution can be found here.
| Attachment | Size |
|---|---|
| Google Add Event example program | 40 KB |
| Google Add Event example VB6 sourcecode | 4.54 KB |

Comments
How to delete items from google calendar using VB
Hi Adam,
I read your article with great interest and I have implemented your code in a Microsoft Access project with the purpose of updating a web based google calendar. It works a treat.
Can you point me in the right direction for deleting events from a calendar. What I'm wanting to do is delete all previous entries on a calendar and then repopulate it with new ones.
I'd be so grateful if you could help me out with some pointers.
Thank you,
Joe Davies
Re: How to delete items from google calendar using VB
Hi,
Glad you found the article useful and the code works for you. Looking at the example calendar API usage page from google I see there is no example for delete, which is kind of strange. However, I think as GCal uses the GData API that this page will tell you how you need to go about deleting an event. Basically I would imagine you can use the same VB methods as we use to trasmit our requests via the Internet Transfer Control, but just change the content of the data we're sending as per Google's instructions. Note the "X-HTTP-Method-Override: DELETE" workaround if you cannot persuade your setup to send a DELETE request directly.
Unfortunately I don't have VB installed any more to test it out as such, but hopefully that will give you a pointer to where to start? If not, get in touch again and maybe we can figure something more out! If that is the case, if it is possible that I could get a copy of your Access application that works so far that would be great as I do still have Access installed.
Good luck, I think it's a shame Google haven't yet made a way of deleting all events in a calendar so that tool alone could be useful!
Adam
MS Access and Google calander
Hi Joe,
I read that you successfully implanted the VB example code into a MS Access form.
This is exactly what I am trying to do, but mine is not working well.
I get errors on the declaration of this tz as CTimeZone. I don’t know from what class this CTimeZone comes (Access seems not to recognized it).
Could you please send me your example that is working? I would help me a lot.
Thanks,
Chris
I tried to create event with
I tried to create event with symbols < or > or ( or ) but thre is the error or there is the wrong values in calendar. %28, %29 instead of ( and )
Code doesn't seem to work
I can't see where the sample code establishes a connection between the Response variable and anything else. When I execute the code, it interminably loops through the Do Events statement without end.
Hi, it's been a while since
Hi, it's been a while since I had a look at this sort of thing, but from memory the place where "Response" changes is in the event Inet1_StateChanged(ByVal State As Integer)
This is an event triggered by the internet control when data is returned, so ought to be triggered when google returns its data (icResponseCompleted ). If for some reason the internet control doesn't think Google has returned its data, then it will indeed loop forever through doevents. This of course would not be a good behaviour for any "real" program!
Calendar Event Delete
Hi, I have copied your code to allow me to create events in a calendar & all is well, but I cannot figure out how to delete them. I have tried the Google Help pages but the expected returns of Calendar URLs & so on just don't happen for me (or I don't know where to look!). Any chance you could explain how to do this in real simple terms? My application is an Access application using VB6. I am pretty new to XML & so on so please bear with me & make no assumptions!
Thanks in advance
Calendar Event Delete
Mick,
I'm having the same challenge as yours: I can create a Google Calendar Event. However, I just can't figure out how to delete / update the event.
Have you had any luck in solving the problem?
PS: When I try to update the event, I get "Content is not allowed in prolog" However, I did validate my XML and it looks correct.
Any help would be appreciated.
Thank you.
Google Calender update through Ms Access 2003
Microsoft Access project with the purpose of updating a web based google calendar
art
I get "Content is not allowed in prolog" However, I did validate my XML and it looks correct.
MSAccess
Hi, I am trying to implement your code in MsAccess but keep getting the message
Run-time error 35764
Still executing last rquest
Can you help please
Jonathan
That work seems interesting
That work seems interesting and fun to some extent but now a days, you get calendars with email clients where you can sync your Google Calendars, it makes it damn easier to add / delete events in there and it all keeps it sync so you can use it on other devices too. But anyways, great work dude! Back then when Calendar just came out, there weren't much better apps to deal with Google Calendar.
St Louis Park real estate