Pages

Friday, October 3, 2014

I have used Google Calendars to schedule and control a number of projects (eg. sprinkler system, alarm clock, etc) . The following How To will get you started. You will of course need a google account.

Create Project

2) Select the project and navigate on the left menu to APIs & auth then APIs and enable Calendar API for this project. You can disabled all other APIs if you only need Calendar access.

3) Next, select the Consent screen menu option from the APIs & auth menu. Enter a Product Name and select an Email Address. If you do not do this step, you will get a Error: invalid_client error later on.

Authorize Application

Next we will need to run our application and authorize it against the desired account.

1) Clone my sample code:

# git clone https://github.com/oeey/gcalendar.git

The sample code is just a slight modification from the Getting Started Sample Code. The Google sample code has some outdated code that will throw some obsoleted warnings.

2) The application has not been authorized to an account yet. Run the application once and you will be asked to paste a validation URL into your browser. Login to your desired target account (with the calendars you want to access) and then paste the validation URL into your browser.

For convenience I have a first_auth.py script that is the same script as the gcalendar.py script, but terminates after authorization. You can run any of the scripts to complete this authorization step.

You may notice the "fakeargparse" code. The run_flow() call wants the flags to be set from the parameters pulled from argparse. I think that is overkill for what I needed, so I just created a fake container so run_flow() wouldn't complain.

Run the first_auth.py script to collect the application authorization.

4) You will be presented with a code, to which you will then enter back into the prompt of the first_auth.py application. The authorization will be stored in the credentials.dat file for future requests.

This script defaults to the primary calendar associated with the account.

Calendar ID

The previous script defaults to the primary calendar associated with the account. If you wish to specify an alternate calendar, you will need the Calendar ID. A calendar's ID can be found on the Calendar Details setting page (same page you can change a calendar's name on). Look for the Calendar Address line, and the Calendar ID will be in the parenthesis. It will look something like "a3sd5221ap2qe5ksbev3ip4@group.calendar.google.com".

Next 12 Hours of Events

Finally, to specify a time range for events, I use the following code in my gcalendar.py script. This code will collect the next 12 hours worth of events.