by Lazy Sloth
Get All Events by Date in Google Calendar API
import os
import logging
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from pydantic import BaseModel
import requests
from datetime import datetime
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)
app = FastAPI()
# The builder needs to provide the Google Calendar API key in the Env Secrets tab.
GOOGLE_CALENDAR_API_KEY = os.environ.get('GOOGLE_CALENDAR_API_KEY')
if not GOOGLE_CALENDAR_API_KEY:
raise Exception("The GOOGLE_CALENDAR_API_KEY environment variable is not set.")
def fetch_events(calendar_id: str, date: str):
url = f"https://www.googleapis.com/calendar/v3/calendars/{calendar_id}/events"
params = {
'key': GOOGLE_CALENDAR_API_KEY,
'timeMin': f"{date}T00:00:00Z",
'timeMax': f"{date}T23:59:59Z",
Created: | Last Updated:
Introduction to the Google Calendar Events by Date Template
Welcome to the Lazy template guide for retrieving events from a Google Calendar on a specific date. This template is designed to help you build an application that interacts with the Google Calendar API to fetch and display events for a given date. It includes endpoints to get events in a list format and to download them as a JSON file. Additionally, it provides a simple HTML frontend to interact with these endpoints.
Getting Started
To begin using this template, click on "Start with this Template" in the Lazy builder interface. This will pre-populate the code in the Lazy Builder, so you won't need to copy or paste any code manually.
Initial Setup
Before you can use the application, you need to set up an environment secret for the Google Calendar API key:
- Go to the Google Developers Console (https://console.developers.google.com/).
- Create a new project or select an existing one.
- Enable the Google Calendar API for your project.
- Create credentials (API key) for your project.
- Copy the generated API key.
- In the Lazy Builder interface, navigate to the Environment Secrets tab.
- Create a new secret with the key as
GOOGLE_CALENDAR_API_KEY
and paste your copied API key as the value.
Test
Once you have set up the environment secret, press the "Test" button. This will deploy your application and launch the Lazy CLI. The CLI will not prompt for any user input as all necessary information is provided through the API endpoints.
Using the App
After pressing the "Test" button, Lazy will provide you with a dedicated server link to access your application. If you're using FastAPI, Lazy will also provide a link to the API documentation.
To interact with the app:
- Open the provided server link in your web browser to view the simple HTML frontend.
- Enter a public Google Calendar ID and a date in the respective fields.
- Click on "Fetch Events" to display the events for the specified date.
- Click on "Download Events" to download the events as a JSON file.
Integrating the App
If you wish to integrate this application into another service or frontend, you can use the provided API endpoints. Here's an example of how to make a request to the API:
GET /events/{calendar_id}/{date}
Replace {calendar_id}
with the ID of the Google Calendar and {date}
with the date you want to fetch events for (in YYYY-MM-DD format).
A sample response from the API might look like this:
[
{
"start": {"dateTime": "2023-04-01T10:00:00Z"},
"end": {"dateTime": "2023-04-01T11:00:00Z"},
"summary": "Event Title",
"description": "Event Description"
},
// ... more events
]
Use the server link provided by Lazy to make requests to your deployed application. If you need to integrate these endpoints into a frontend application, you can make AJAX calls to these endpoints and process the response as needed.