Google Calendar Automation in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to automate Google Calendar with python so let us get right into it [Music] alright so we're going to learn how to use the Google Calendar API in Python today and this can be useful for a number of different use cases for example maybe you have your own personal management system written in flask or Django where you manage your whole life where you have all your projects and then you want to export some event or some appointment into Google Calendar or you want to import something from Google Calendar you want to automate certain processes or maybe you have some data set with a lot of events a lot of appointments and you want to turn all of those into Google calendar events but you don't want to do it manually that would also be a use case so you can be creative with that we're going to learn how to do this in this video today and for that we're going to use the python quick start guides at developers.google.com you'll find a link in the description down below hopefully um we're not going to follow it exactly we're not going to read anything here uh specifically I'm going to explain everything myself but we're going to use this here as a guideline so that we don't skip any important steps here essentially if you watch any of my other if you have watched any of my other tutorials on the Google API so the Google Sheets automation or the Google Drive automation the process is the same the authentication process is the same but of course we're using a different API now so what you need to have already is you need to have python installed you need to have Pip installed and we need to create a Google Cloud project so you can click on this link here a Google Cloud project and then you can go to the developers portal and we can create a new project here we're going to call this now calendar YouTube neural 9 or something like this then we're going to create this project and then this is now our project it's important that we enable now the Google Calendar API as of right now we cannot use it because it's not enabled so you can either do it here yourself in the Google Cloud interface or if you don't know where to find it you can just scroll down here and click in the guide on this enable the API button then you just need to make sure if you have multiple projects the correct project is selected here and then you want to say next enable and then you have the Google Calendar API enabled then you can scroll down and it tells you that you should get credentials now we need to authenticate ourselves with the API we need to say okay I'm using this project I want to do this and I allow this to happen and because of that we need to create credentials and for that if I remember correctly we need to First create an oauth consent screen so we're going to click on oauth consent screen external create then we're going to give the application a name so for example Google calendar or what was the name calendar YouTube app or something like this support mail is going to be just my Google email here I'm not going to upload anything that's optional contacts also going to be here social at neural9.com save and continue and then we get to an important step the Scopes now scope is essentially um what you want to do with the application so which permissions the application should have and what we need is we need a scope called calendar for this of course it has to be enabled but we just enabled it so we're going to just look here for googleapis.com auth calendar this is what we need we're going to check this we're going to update I'm going to save and continue we need to add ourselves as a test user otherwise I think we cannot use this we're going to click on ADD I'm going to save and that should be it so we can go back to the dashboard then we can go to credentials and we can create a new oauth client ID and here we're going to say that our application is going to be a desktop application and we're going to call this calendar YouTube desktop choose whatever name you want create and the important thing now is you have a bunch of keys here you should keep them secret so you should not share them with anyone what we need here is we don't need any of those keys we want to download the Json file so we're going to just click here on download Json this is going to download um a Json file with the credentials I'm going to copy this and I'm going to navigate to my uh working directory here so I'm currently working here I'm going to copy paste this I'm going to rename this to credentials.json and we're going to use this file here in our application that is what we need to do in the Google Cloud API or in the Google Cloud developer console now what we're going to do is we're going to write the code for the Automation and in my case I'm going to use the calendar here this is just the calendar that's associated with my Google account I don't use it really because I have my personal calendar somewhere else but we want to create certain events like this so we want to have different colors different names and stuff like that we want to invite people automatically with python this is what we're going to do now um and for this we're going to just create a python file main.py and of course one thing that I forgot is we need to install here A bunch of packages so you can go here to install the Google client Library you can just copy this installation command here so you just open up your terminal or your command line so on Windows that CMD on Linux that would be just a terminal on Mac as well and here you can just copy paste this in the case of a Linux system you have to provide pip3 instead of pip and then you can just install everything in my case it's already installed and then we can use the respective packages let me just stick my prepared code here to the right side of the screen so that I can look at it we're going to start by importing date time as DT and basically for those of you who want to uh have some code on their second screen if you have one while watching this video you can also look at this code here I'm going to use I'm going to basically use that code but I'm also using a more advanced example because I think here it doesn't even create a new event so I think this code here from the tutorial just gets the events and lists them what we're going to do is we're going to do that and we're going to create our own event so this code is a good um what would you say good template but it doesn't really do much so it also has only the read-only permissions we're going to have the full calendar permissions um so we're going to import date time as DT we're going to import os.path and then we're going to import all these Google packages so from Google underscore we're not underscore google.auth dot transport dot requests from here we're going to import request then from Google oauth 2 dot credentials import credentials this is why uh this is what we're going to use to load the credentials file then from Google auth oauth lip dot flow we're going to import the installed app flow then from Google API client dot Discovery we're going to import build and then from Google API client errors we're going to import HTTP error that's basically it and then we need to define the Scopes in our case we only have one scope which is going to be https colon slash slash www.googleapis.com off slash calendar like this and we're not going to use read only because we also want to create events this is why we added the correct Scope when creating the oauth consent screen and then we're going to Define just a simple main function here and we're going to initialize the credentials as none and we're going to then say if os.path exists token dot Json the basic idea is we're going to use the credentials to authenticate ourselves we're going to create then a token so that we don't have to every time use the credentials and confirm that we want to log in we want to have all the permissions uh we can just use the token but if the token does not exist of course we need to create it so we're going to say credentials are equal to credentials Dot from authorized user file which is going to be token so in this case we have the token sorry so what we're doing here is if the token exists we're going to load it from the token file otherwise we have to log in the conventional way and then we have to create this token file so if we don't have credentials or if the credentials are not valid so this is an if statement that basically triggers if we don't have a token Json or if the token is invalid so maybe expired or something in this case we're going to say if we have credentials so basically if the credentials are invalid this is another way of putting that and the credentials are expired and uh credentials.refresh token then we're going to just credentials dot refresh and then request like this so basically this is just the code from this nothing that you need to think about yourself this is just a code here from the Google example of how to authenticate this is also what we're going to use here from from this code from the tutorial then we're going to create a service and from then on it's basically the logic that is specific to the API so we're going to say here otherwise flow is going to be installed app flow so if we're creating this for the first time from client Secrets file credentials.json with the scope so we want to have these permissions and the credentials are then going to be flow dot run local server Port equals zero and then in the end what we want to do definitely is we want to say by the way this needs to be indented sorry and this is well with open token.json in writing mode cast token token right threats to Jason that's basically the authentication and then with all of this we're going to try to do something we're going to accept here uh what we're going to wait for is an exception which is just the HTTP error that we imported in this case we're just going to say an error occurred let's just how you write a card like this right and we're just going to say it's the error and what we're going to try is we're going to try to get the calendar service so we're going to say um service equals built and we're going to say essentially calendar version three and the credentials credentials equals creds like that and then we're going to just get a timestamp for right now so that we can actually get uh the next couple of uh events so the first code that we're going to write this is also from this tutorial but then we're going to write some code ourselves uh is to get the 10 upcoming events so this is basically the base code of getting the 10 upcoming events um just looking at the next events and getting 10 of them so I think right now I don't really have upcoming events so I'm going to create some of them and I'm going to do that in the next week here I'm going to just create a bunch of oh boy what did I do now let me just go to the week view again uh there you go we're going to create a couple of events here just using some random stuff so that we can we have something that we can list here how many of those now we have five right ten two times five uh yeah that's basically it and now we're going to say we have the current date time which is daytime now this is important because of course we want to look into the future not into past events we're going to say here when I have this in ISO format plus Z and then we're going to get as event underscore result service dot events list calendar ID is going to be primary so basically just the ordinary calendar the time minimum so the earliest point we're interested in is now so this timestamp here and the Max results are going to be equal to 10 because we want to have the 10 upcoming events and then single events we want to have this we want to set this to true and then the start time or we want to say order buy equals oh by the way sorry about this I'm blocking with my camera the code so order buy is going to be start time like this let me just zoom out a little bit so you can see the full line here event result is Service events list calendar ID primary time minimum now Max results 10 single events true order by start time and this is going to give us the 10 upcoming events and what we want to do now is we want to say events equals event results or result get items and an empty list and then we can just say if not events print no upcoming events found and return and otherwise if we don't return we're going to say for event in events print I actually want to have first of all the start time so events start dot get date time and then event start get date and then when a print start and the summary of the event so if I didn't do any mistakes but I didn't make any mistakes this should already work uh let me just see here oh of course I forgot to run main so we need to add here if underscore underscore name underscore underscore equals equals underscore underscore main underscore underscore then just run Main there you go so now I have this auth uh screen so authentication screen here I click on neural nine and it's going to ask me for permission I'm going to click here on continue I'm going to continue I'm going to allow the X's you can see flow has completed uh we have the problem with get where is that event result get items this is a problem why is that oh because I forgot to add the execute command at the end here now you can see we get the 10 upcoming events I can also set this probably to where is the line again I can probably also set this to 12 and we're going to see uh that I have I have some birthday in here that I for some reason have in this calendar but of course we can adjust this number so I can set this to five there you go so this is how you get events this is how you list events now we're going to do something that's not part of the Google tutorial here so you will not find this in the quick start section here we're going to create our own calendar event which is in my opinion uh more useful than just listing events we want to be able to create events maybe from a data set maybe from our personal management system automatically so I'm going to remove all of this here and then what we're going to do is we're going to create an event object so we're going to say event equals and this is going to be a dictionary now you will need to Google your three uh you you will need to Google your way through some of the uh conventions and the keywords because this is not something that you can just figure out in your own brain you need to look into the documentation to figure that out basically to create an event you need to use a bunch of key value pairs you need to provide a bunch of key value pairs so you need to say summary is location is start time is end time is and stuff like that you just have to look into the documentation to do that if I don't forget it you will find a link in the description down below but you can use this example here and adjust it to your needs every event has a summary this is basically just a name the title that you specify when you create a new event in our case this is now going to be my python event then we're going to have a location for this event which is going to be somewhere online this time then we're going to have a description this is a more detailed description so some more details on this awesome event then we're going to specify a color ID so for example we will go with six now which color is which number is something you have to look up again you can play around with this and experiment uh but we're going to use color 6 now and then we can change this to see what what changes I don't know exactly now which color 6 was but probably red or something then we're going to provide start and start is going to be its own dictionary with a date time object which is going to be or a daytime key value pair which is going to be 2023 Dash uh what's today May 26th so we're going to go with June 2nd for example uh then we're going to specify a t to specify a timestamp which is going to be 0 9 colon 0 0 colon zero zero and then we're going to provide a UTC offset because I'm in Europe and Central Europe and Vienna I'm going to provide zero to colon zero zero so basically the date in year month and day format and then T then 9 0 0 so basically hour minute second and the UTC offset of two and then we have the time zone which is going to be Europe slash Vienna we're going to copy this we're going to paste this and we're going to rename this to end and the end is going to be essentially at 17 o'clock so 5 p.m basically same time zone we can also add now re recurrence so basically if this is a recurring event or not so recurrence we can say is uh and this is a list now and here we specify a rule recurrence rule which is going to be frequency equals daily and count is going to be three so it's going to be daily for three days and then we can say here or I hope this means for three days and not every three days I I think this should be correct like this we can see here uh what happens in a second and then we can add attendees if you want to and each of the attendees is going to be its own dictionary and we're going to have in here email social at neural nine dot com and then we can copy this and we can say I don't know some email that hopefully does not exist at mail Dot com something like this and that is basically the event now in order to create the event what we're going to do is we're going to say event equals service dot events insert into calendar ID primary the body is going to be event we're going to execute this so this then turns into it turns this into an event and then we can print event created F string and we're going to say event dot get HTML link with single quotations because we're in an S string here there you go this should work I think and there you go event created we can now go into the Google Calendar and you can see here my python event I think this is uh orange is a color and the reason why it's white is because I think we didn't accept the invitation if I say that I'm going if I say yes it's going to turn into orange there you go and yeah so all events then all of them are going to turn into orange and we can see that this goes for three days so it's daily three days you can see the color orange because that was apparently number six so I don't know what the the pattern is here you can you can look that up in the documentation and when we click on this you can see here somewhere on online is the um the location this is the summary this is the description and we can see here the two guests for this event and you can use this blueprint here and you can also see the time it's June 2nd from 9am to 5 PM and you can use this blueprint now this pattern this template to essentially um automate your own calendar system so you can have your own calendar application written in Django written in flask and you can interact with the Google API um the way you want to or you just have some as I said uh already you have some data set that you want to use to create multiple events and to invite people you can do that easily with a Google Calendar API so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 35,656
Rating: undefined out of 5
Keywords: Google Calendar API, Google Calendar integration, Google Calendar Python script, Google Calendar automation tutorial, Google Calendar Python API, Google API integration, Google Calendar API tutorial, Python Google Calendar automation, Google Calendar automation script, Google API authentication, Google Calendar API Python example, Calendar automation using Python, Python Google Calendar integration, Google Calendar API authentication, Google Calendar API tutorial for beginners
Id: B2E82UPUnOY
Channel Id: undefined
Length: 25min 9sec (1509 seconds)
Published: Fri May 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.