User1
User1

Reputation: 85

Get Google Calendar events by given date using Python

I got the following code for fetching all events from a specific calendar. This code works for me. I would like to add a variable to get the events of today's date.

I'm using the GET HTTP Request: https://www.googleapis.com/calendar/v3/calendars/calendarId/events from developers.google.com/calendar/api/v3/reference/events

My code:

import requests

url = "https://www.googleapis.com/calendar/v3/calendars/***calendarId***/events"

payload={}
headers = {
  'Authorization': 'Bearer *************************************************'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

Upvotes: 0

Views: 1088

Answers (1)

Daniel
Daniel

Reputation: 3745

Building on the comment already posted, you can refer to Google's events list documentation. It explains that you can use the timeMin and timeMax parameters to specify a minimum and maximum date. Note that you have to enter the date in RFC3339 format or you will get an error. Here's an example based on your code:

import requests

url = "https://www.googleapis.com/calendar/v3/calendars/***calendarId***/events"

headers = {
  'Authorization': 'Bearer *************************************************'
}
params= {
    "timeMin":"2022-09-19T00:00:00-06:00",
    "timeMax":"2022-09-19T23:59:59-06:00"
}
response = requests.request("GET", url, headers=headers, params=params)

print(response.text)

You don't need a payload for this method so I removed it from the sample. As explained in the documentation, the offset is mandatory so you have to keep that in mind. There are many ways to generate the datetime, but you can refer to this answer for some samples using the datetime module.

Upvotes: 1

Related Questions