0

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)
User1
  • 85
  • 2
  • 15

1 Answers1

1

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.

Daniel
  • 3,157
  • 2
  • 7
  • 15
  • Great. That works for me! Is there anything I can do to stop the API connection failure (due to expiry after ~60 min)? – User1 Sep 20 '22 at 09:54
  • 1
    Glad to help. If you set `access_type=offline` in the parameters when you generate the access token you will also get a refresh token. Then you can use the refresh token instead of an authorization code to request a new access token after the 60 minutes have expired. You can check out [this answer](https://stackoverflow.com/a/38414217) for a sample and [here's](https://developers.google.com/identity/protocols/oauth2/web-server) the official documentation as well. – Daniel Sep 20 '22 at 13:19
  • I've added into the **params** scope the `"access_type" : "offline"` after the: `"timeMin":"2022-09-19T00:00:00-06:00","timeMax":f"{today_date}T23:59:59-06:00",` but it still expires after 60 min. – User1 Sep 20 '22 at 13:25
  • My script should run every day once a day. – User1 Sep 20 '22 at 13:27
  • 1
    Check out the docs to see the entire workflow. You need to add this at the beginning, in the part where you generated the first `access_token` that you are using as `Bearer`, then the response will include the refresh token in addition to the access token that you already have. That part is not included in your sample code. – Daniel Sep 20 '22 at 13:30
  • I created a new question. I really tried reading all the relevant Docs but still, wasn't succeeded. [How to auto refresh Google calendar token in python](https://stackoverflow.com/questions/73788841/how-to-auto-refresh-google-calendar-token-in-python) – User1 Sep 20 '22 at 15:11