Wednesday, January 4, 2012

[Gd] Automatically setting a vacation responder from a calendar event

| More

Google Apps Developer Blog: Automatically setting a vacation responder from a calendar event

Are you ever at an airport with no internet access, and realize that you forgot to set your "Out of Office" (OOO) message?" I forget regularly, but always remember to block off my calendar. Why not have the calendar automatically update my OOO message? We can! With the Calendar API, we can retrieve calendar events and set a vacation responder for the event duration for any user in the domain using the Email Settings API.

The first step is to authorize the Calendar and Email Settings client to make any call to the APIs respectively. The new Calendar API requires google-api-client library while the Email settings API still works with the gdata-client library in Python. These libraries use different underlying protocols and handle authorization differently.

Fortunately we can use the same code to get an OAuth 2.0 token for both the Calendar API and the Email Settings API.

from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from import run

SCOPES = (' '

FLOW = flow_from_clientsecrets('client_secrets.json', scope=SCOPES)

storage = Storage('vacation.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(FLOW, storage)

Now that we have obtained the OAuth 2.0 token from OAuth2WebServerFlow, we can use it in either library. To authorize the Calendar service:

# Create an httplib2.Http object to handle the HTTP
# requests and authorize it with our good Credentials.

http = httplib2.Http()
http = credentials.authorize(http)

# Build authorized service for Calendar API
service = build('calendar', 'v3', http=http)

Authorizing the Email Settings client requires adapting the credentials:

auth2token = gdata.gauth.OAuth2Token(client_id=client_id,

email_client = auth2token.authorize(

Now its time to update the vacation responder based on calendar event using the authorized client. Lets query for events containing the string ‘vacation’ from the Calendar API. The following code snippet shows the retrieval of events.

# Convert the date to the RFC 3339 timestamp format
# for Calendar API.
cur_datetime =
cur_date = cur_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')

events =,
q='vacation', timeMin=cur_date, singleEvents=True,

By specifying a user’s email address as the calendarID, we can query against any primary calendars in our domain that are shared and readable by the administrator. If the user opts to hide their calendar, we won’t find any of their events. For additional details about the query on calendar events, refer to the calendar query parameters from the reference guide.

Next we can obtain the start and end time of the event and update the vacation responder using the Email Settings client if the event’s duration is day long or more.

if 'items' in events:
for event in events['items']:
startDate = event['start']
endDate = event['end']
# If event is set for the day not just for a time slot.
if 'date' in startDate:
subject='Out of office',
message='If urgent call me.',
print '\nVacation responder set for the days between %s to %s' %
(startDate['date'], endDate['date'])

You can download the sample and build your own application on top of it. We hope that this sample makes it easier to get started, particularly for apps that need to combine APIs from the two API clients. Please feel free to reach us in the Google Domain Info and Management Forum with any questions you have or write us feedback on this post.

Shraddha Gupta   profile

Shraddha is a Developer Programs Engineer for Google Apps. She has her MTech in Computer Science and Engineering from IIT Delhi, India.


No comments: