Access Google calendar data with Python

Google allows everyone to create multiple private or public events or calendars in Google Calendar. So using the google calendar API’s you can create or manage those events in a programmatical manner. If you’re creating any desktop reminder app or any event managing app or any kind of chatbot, then google calender integration will be quite useful for you.  So in this article, we will see how to access or manage google calender using google calender API in Python.

Getting Google Calendar API

So, first of all, you need to go to the Google Developer Console to make a new project or select an existing project. After choosing any existing project or creating a new project search for Google Calendar API

Access Google calendar data with Python

Now there you need to enable that API

enable that API google calendar

After that, you need to create some credentials to use that API inside our program. So choose credential from the left menu bar, then choose Create Credential option. So before going further let me tell you, credentials can be in multiple forms, for example in API form, or in OAuth Client Id, etc. Since we are integrating Google Calendar with Python, we will go for OAuth 2.0 Client ID. To create an OAuth Consent Screen, then click on External, and then give an application Name. So, for example, I’m Giving ‘Test Calendar’ and then save it. So after creating the Oauth Consent Screen, goes back to Credential and now click on Oauth Client ID, and select application type to others and click on CREATE.

Then it will show you your Client ID and Client Secret. But we don’t need those, we actually need some JSON files. So click on the download option as per the image given below:

Client ID for google calendar

It will download a JSON file on your device. Now you can rename the file with a shorter name so that you can use that easily. So I will just rename it with client_secret.json, once you have done that, you can open your Jupyter Notebook(We are preferring Jupyter Notebook to do this), and install google-api-python-client. Just go to your Jupyter Notebook through the terminal Terminal.

install google-api-python-client

install google-api-python-client

How to Access Google calendar data with Python

After the installation of the Google API Python Client, we need to set up OAuth 2.0. So write

from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

If you find any error for 2nd line then try to install google_auth_oauthlib separately.

pip install google_auth_oauthlib



Actually there are different types of flow available to use in different scenarios. We are using InstalledAppFlow here.

Now we need to set up scopes, for that, you can go to Google Calendar API’s auth section
and there you’ll find all API’s for OAuth 2.0

find all API's for OAuth 2.0


Since we want to access the events as well as the calendar we will go for the first one, if you want to be more restricted you can go with the third one where you’ll have permission for events only. Now write

scopes = ['']

And now we need to create a flow:

flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", scopes=scopes)
credentials = flow.run_console()

Now if you’re using Jupyter Notebook You’ll see this kind of section:

flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", scopes=scopes)

It will ask you to open that URL and sign in to that account, so click on the first URL, and choose your account and sign in. Then you’ve to give permission to this particular google project for edit, share and permanently delete all the calendar. Click allow, and then it will give you a token. Copy that token id, and paste that into Jupyter Notebook. Now you have the credentials into your Jupyter Notebook. Type:


You’ll Get your credential details there:

<google.oauth2.credentials.Credentials at 0x1c3....48>

Now if you want to use these credentials multiple times you need to run the above programs every time, yeah that’s pathetic but true. But if you save these details, into a pickle file, then you can just call the pickle files, and it will run.

import pickle

pickle.dump(credentials, open("token.pkl", "wb")) 
credentials = pickle.load(open("token.pkl", "rb"))

so import the pickle module and the dump the credentials into any file name, I wrote here token.pkl and wb means write Binary mode.
Now if you want to call the pickle file or you can say read the pickle file, write the second line with rb i.e. read Binary Mode.

Hence from now onward I don’t need to sign in every time for my credentials, I can just run the pickle file and it will give me my credentials.

Now we have to create a service object. So write the following code. Remember to check whether you’re using V3 or not.

service = build("calendar", "v3", credentials=credentials)

So you’re now ready to access the Google Calendar. If you ever stuck in any of the steps you can always go to Google Calendar Reference section and choose the Calendar List and click on lists. Calendar List google Python and go down a little bit, you’ll find a code for Calendar List

So there you can clearly see that we need to use service.calendarList().list() to access the Google Calendar. So let’s do that:

result = service.calendarList().list().execute()

Now if you run result:


It will show some calendar presents on that particular Gmail ID with which you made this project in Google Developer Console
project in Google Developer Console

Now if you just wanna see the first calendar, then write:


If you still have doubted what is the calendar, Just see this image:project in Google Developer Console

Since we saw our calendars, now it’s time to see our events. For that write the following codes:

calendar_id = result['items'][0]['id']
result =

It will show you the first event on your calendar, If you wanna see all the events, then like the previous method, print only results

How can you create a new Calendar Event in Python

Now, here comes the interesting part i.e. creating a new calendar event directly from your Python Code. So for that, just go to insert API under the event section How can you create a new Calendar Event in Python
And there go down a little bit and you’ll see the python demo code, and you’ll get a little bit of understanding, like how we are going to do this.

So “events” is a variable name that contains a dictionary. We’ll just copy that code from the API section and customize it accordingly.

  • In the Summary section, we will write the Event Name.
  • Then write the location of the event.
  • Then a short Description
  • After that start time and end time with proper Time Zone. (It is compulsory to give this one, as it will create an event in Google Calendar)
  • In the recurrence section, writes how many days the event will repeat. If there is no repetition, just delete that section.
  • Now in the attendees’ section, you can invite anyone through email, if they’re participating in the event also.
  • Now in the reminders section, you can set a reminder with a time before through email or a pop-up notification on the mobile.
event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  'recurrence': [
  'attendees': [
    {'email': '[email protected]'},
    {'email': '[email protected]'},
  'reminders': {
    'useDefault': False,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},

now,'primary', body=event).execute()

If you run the above cell, it will show you the recently created event

So yeah, in this way, you can create an event directly from Python Code, and also you can see all your calendars, events of Google Calendar in Jupyter Notebook. You can try other properties from the Google Calendar Developer Reference Section to modify the events or calendars.

Also read:

4 responses to “Access Google calendar data with Python”

  1. David says:

    First guide which make me able to add event on calendar !

    Great job, thanks you so much.

  2. Martin Loetzsch says:

    Thanks for the tutorial, helped me a lot

  3. Seppl says:

    Any idea how to solve the issue, that a token for a project in Testing mode will only last 7 days? Seems to be the case since the beginning of the year.

  4. Tejasri says:

    excellent. I searched many Websites and youtube videos, but there is no use. tqq I had done it.

Leave a Reply

Your email address will not be published. Required fields are marked *