Hey, everyone, here a startling revelation.
Did you know creating calendar events can
enhance your user's experience and bring about better
and higher engagement?
This is Wesley Chun, and by the time I'm done,
you'll be wondering why you haven't already
implemented this in your web or mobile apps.
For developers, you'll discover it's easier than you think.
Your users will save time and think, yay, what a cool app.
I don't have to do it myself.
To get started, let's go somewhere
where you can have such an experience.
Going out with friends to have a good time is always
a great experience, whether it's a night at the movies,
going to the game, or eating out.
Let's say that you've built a dining reservation
app to simplify setting up such events.
Besides booking the reservation, it
notifies guests and handles RSVPs-- very useful.
But sending email is easy.
How can you make this experience even better?
All right.
Truth is, while your app helps users with scheduling,
they're not using your app for appointments.
They're using a calendar.
Wouldn't it be great if your app handles the booking
and adds the events to their calendars?
Users will love your app because they
don't have to manually add those events to their calendars.
Time and effort saved for the win.
And that's how you take a good experience and make it great.
Today's calendar-- people rely on it
to organize their lives, at work and at home.
When booking events using your app, whether it's
a dining reservation, plane ticket, hotel
room, a night at the game, or a concert,
it's an extra chore for people to get it into their calendars.
So how do you automate calendar events then?
With the Google Calendar API.
It sounds complicated, but really, it's
easier than you think.
To help you get started, let's look at a short Python script
that creates an event with a minimal amount of information
and code.
At the end, I'll have a link to the docs where you can see
similar examples in Java, .NET, PHP, and more,
including mobile environments like Android and iOS.
Ready?
Let's go to the computer now.
OK, let's start at the top.
We need to import the print_function for Python 2
users since it's the default in 3.X.
The goal is to have code that runs in both Python 2 and 3.
The Google Calendar API is another authorized Google API,
so the remaining imports are the same to access
any authorized API, just like we've
seen in previous episodes.
What does change, however, are the scopes.
We're now using Calendar, thus need one of its scopes.
Currently, the Calendar API has a read-only
and a read/write scope.
Since we're adding a new Calendar event,
we need read/write, which is this particular scope.
The rest of the boilerplate remains the same.
Completing the OAuth process, where if successful,
ends with granting this script the ability
to access the calendar data of the user who
gave the authorization.
With permissions granted, we can now create a service endpoint
to the Calendar API.
Use calendar as the API name, v3 as the current API
version-- at least it was when we recorded this video.
Finally, pass in the HTTP connection objects
so requests are signed with your credentials.
In order to create an event, we need
to call the API with the necessary event details,
so that's next.
The GMT_OFF variable is for my local time zone, which
is Pacific daylight time, or a mountain standard time,
which is GMT minus seven hours, but you should use your own.
The data in the EVENT variable is the bare minimum, an event
name plus start and end times.
Now that we've got the data that we need,
it's time to call the API with the service endpoint
CAL that we got earlier.
We'll use the event object's insert method.
The calendarId is the ID of the calendar
to insert the event into.
The special name primary refers to the currently
authorized user so that's what you'll typically use.
sendNotifications is the flag that
says send an email notification of the new calendar event.
If you're inviting friends, it's probably nice to let them know.
Finally, pass in the event details as the body
and execute the API call.
Now, this will work, but how can you
tell the event was successfully added?
Well, you can't unless you check the return value, which
is this variable e up here.
It's the event object that serves as a receipt containing
the event details, which are then displayed to the screen
using the print call down at the bottom.
All right, now we're ready to save and run it.
And as you can see, we get the familiar OAuth prompt.
So this is the actual action that
lets the user authorize that script access to their calendar
data.
So we're going to allow.
And you can see here that the print function has completed,
which means there should be a dinner with friends
on our calendar.
And there it is.
That's great.
Whoops, we forgot to invite our friends.
If you look at the event, there's no friends in here.
So unfortunately, we've got to delete it and do it again.
So how do we add friends?
Well, you go back into the code and, particularly the event
data, and add a list of attendees,
just giving them the email addresses for all your friends.
All right, now if we save and run it again,
we'll get more success
OK, it looks like the event has been added again.
And this time, your friend should
have gotten an email notification,
besides the calendar event, which you can see
here along with some friends.
It even works for non-Google users, such as my friend here
who has an AOL account.
If you click through, you'll see the user interface isn't as
rich, but they can still RSVP.
So that's it.
Cool.
You've programmatically created your first Google Calendar
event.
Wow, that's it, all in less than 40 lines of code.
As you can see, it doesn't take much to add a calendar event,
meaning it doesn't take much effort
to give your users a seamless and delightful experience.
Inserting events is just the beginning.
Think of the API as a way to really open up
the power of Google Calendar and integrating its features
into your app.
It will be able to update and delete events, not just add.
If your events have location information,
users can get Google Now cards that tie in with Google Maps,
providing time and proximity info.
Adding events that repeat, like monthly dinners and book club
meetings, is a cool thing.
Attachments are supported so you can add event-related documents
like a PDF of the show tickets.
Where do you go now?
Well, the first link gets you started with the official docs.
Want to enhance the email you're already sending to users?
Well, check out the episode at link number two.
Finally, down at the bottom, are links
to connect you to more episodes in this and other Google
Developer video series.
By intelligently and more completely
managing your users' overall experience,
you simplify their lives by saving them time and effort,
and they can rest assured they won't miss the event, leading
to a high rate of engagement.
In turn, they will thank you by continuing to use your app
rather than the competition's.
Leverage the power of the Google Calendar API
and see how it can take your app to the next level.
I'm Wesley Chun from Google and we'll
see you the next time on the LaunchPad Online.
[MUSIC PLAYING]