[Music]
guten tag developers those who speak
German no that means good day
speaking of days did you know you could
use email mark up and the Google
Calendar API so your apps can create
calendar events on behalf of your users
well it's true
I've got code and videos to prove it the
plans change and wouldn't be awesome if
you could also update events when plans
change or create repeating events
welcome to the G suite dev show I'm your
host Wesley Chun today we'll show how
your apps can update existing events
with a calendar API and as a bonus make
them repeat too in the video on email
markup I described how it helps Gmail
automatically add events to your users
calendars it's simple and doesn't
require you to write any code you're
only adding markup to messages you're
already sending to users check out the
video and email markup Doc's to see how
to do this with the links down below but
as a developer you may want more control
and not rely on another app to do the
work what if you had a typo in your
markup or what if your user's email
clients don't process markup no calendar
event instead your app can explicitly
insert events into your users calendars
here's another video where I show you
how to use the calendar API to set up
dinner with friends check out the video
and blog posts to review the JSON
payload used to create that event well
here's the JSON payload in Python
dictionary format from that video the
event variable is how we ask the API to
create that initial dinner invite to
begin with it's got the event name the
start and end times and invitees this
entry or record is the API resource for
this event by the way what do you think
the are and URL stands for well we're
happy that dinner worked out but imagine
another dinner that didn't leaving you
with an unfulfilled calendar event but
wait today you got the good news that
your friend got assigned a job nearby
for the rest of the year not only do you
want to get that dinner back on track
but also want to make it up by monthly
thing until the end of the year this is
a perfect feature to update your app
with it saved the original event ID from
the dinner that didn't work out and now
it only needs to be updated with the new
dates and to make it repeat the calendar
API gives you two ways of updating
events you can either patch meaning
updating one or more fields of the event
resource or update meaning recreate the
entire
resource we'll look at both and discuss
when you'd pick one over the other to
update individual fields use the events
that patch method named as such because
you're patching the updated fields you
know that Delta's from the original
resource in our case we got to patch the
new date into the event also we're now
using a time zone instead of a specific
offset from GMT this allows for events
to observe daylight savings time meaning
at 7:00 p.m. dinner stay the 7:00 p.m.
as we crossed fall and spring boundaries
you can still use an offset with a time
zone if you wish as they are independent
of each other something you haven't seen
before is how to do repeating events to
do this you need to define what's known
as a recurrence rule which answers the
question of how often an event repeats
it looks somewhat cryptic but follows
the RFC 5545 internet standard which you
can basically decode like this the event
has a frequency of occurring monthly but
with an interval of two meaning every
other month till the end of the year
more in following examples at the RFC
link down below the other alternative to
modifying an event in a calendar is
update meaning replacing the whole thing
this is likely the best choice if you're
updating most or all of the fields
anyway create a new resource with all
the existing and updated fields as if
you are starting from scratch then call
events update to override the whole
entire resource since we only need to
update the date and add a recurrence
rule which of the two choices seems
better yep patch because we're only
modifying two fields which you choose
depends on your situation though because
if you want to change the event name or
have a whole new group of friends you
probably choose updates interchanging
most of the fields right by the way
regardless of whether you use patch or
update in a real app your user may make
changes from their desktop or mobile
device and typically you want to avoid
conflicting changes or race conditions
so we recommend using the if match
header and an e-tag string to uniquely
identify updates for more info on the
ladder
check out the conditional modification
page in the docs the codes in Python to
keep things brief but feel free to use
the client library for your favorite
language create a project in the
developers console with a calendar API
in a table then create an event in
Google Calendar and grab its ID check
out the previous video if you don't know
how to do that once you're set
we can go to the computer now and look
at some code to modify that away lines 1
through 13 is the standard boilerplate
we've covered in many of our other
videos but here pay extra attention to
line 7 which is the read/write scope for
Google Calendar and line 13 which is
where we create the service endpoint to
the Google Calendar API as mentioned
earlier we chose patch in this example
because we're only changing the date and
making this event repeat the timezone on
line 15 is immediately followed by the
event payload on line 16 through 20 you
need to change line 21 for your event ID
22 and 23 send this request to the API
with a call to events that patch finally
the print call at the end gives us a
receipt showing the user the modified
event including the newly installed
recurrence rule all right now let's run
this thing since I've personally run
this before we won't see the aw2 prompt
here but you'll get it on your first try
the onscreen output shows a new event
information but as you can see it showed
up on our Google Calendar to tada
obviously the script I ran had a real
event ID but the rest of it is exactly
what we just covered on-screen code that
modifies calendar events and making them
repeat all in about 30 lines of code
anyway take a look at the deep dive post
if you want to dig into the details your
next steps check out the official Docs
for everything you need to know if
you're new to the calendar API
specifically check out the concepts and
overview page the final resource is a
guide on calendar and event objects even
covers recurrence armed with this
newfound knowledge you're now well on
your way to giving your users an even
better experience by enhancing your app
with a Google Calendar API let us know
how it works for you down in the
comments below and give us ideas for
future videos this is Wesley Chun and
we'll see you upstairs in the D suite
[Music]
you