I built my own Google Calendar Event Scheduler with FastAPI

Rasel HasanRasel Hasan
3 min read

Hey Codemonkey!

I'm introducing a simple FastAPI-based application that allows users to schedule and manage events using the Google Calendar API. This app integrates Google OAuth for authentication, enabling event creation and retrieval from a user's Google Calendar.

Features

  • Google OAuth2 for user authentication.
  • Fetch upcoming events from the user's primary Google Calendar.
  • Create new events on the user's Google Calendar.
  • In-memory token storage for authenticated users (single-user session) to keep the project simple.

Project Structure

google-calendar-event-scheduler/
│
├── app/
│   ├── api/
│   │   ├── auth.py            # Handles authentication routes
│   │   ├── events.py          # Handles event-related routes
│   │   └── __init__.py        # Imports FastAPI routes from different modules
│   ├── core/
│   │   ├── config.py          # Configuration for environment variables and settings
│   │   └── auth_flow.py       # Google OAuth flow setup and helper functions
│   ├── models/
│   │   ├── event.py           # Pydantic models for request/response schemas
│   │   └── __init__.py        # Imports all models
│   ├── services/
│   │   ├── calendar.py        # Calendar service interactions
│   │   └── __init__.py
│   ├── __init__.py            # Initialize FastAPI app
│   └── main.py                # Entry point for the application
├── .env                       # Environment variables, ignored from git tracking
├── .env.example               # Environment variables example
└── requirements.txt           # Dependencies

Pre-requisites

  • Python 3.12

  • A Google Cloud project with Google Calendar API and OAuth credentials.

  • Virtual environment (venv)

Setting up Google OAuth

  1. Go to the Google Cloud Console.

  2. Create a project and enable the Google Calendar API.

  3. Set-up OAuth 2.0 credentials: Authorized redirect URIs should match the one used in the .env file.

  4. Download the OAuth client credentials and place them in your .env file.

Environment Variables

  • Create a .env file in the root directory with the following contents same as .env.example file:
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REDIRECT_URI=http://localhost:8000/auth/redirect
GOOGLE_SCOPES=https://www.googleapis.com/auth/calendar

Installation

Clone the repository:

git clone https://github.com/raselhasan111/google-calendar-event-scheduler.git
cd google-calendar-event-scheduler

Create and activate a virtual environment:

python3 -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`

Install dependencies:

pip install -r requirements.txt

And set up your environment variables in .env (as shown above).

Running the Application

Start the FastAPI app using Uvicorn:

uvicorn app.main:app --reload

The app will be running at http://127.0.0.1:8000.

Endpoints

  • Login with Google OAuth
Endpoint: /auth/login
Method: GET
Description: Redirects the user to Google's OAuth2 login page.
  • Handle OAuth Redirect
Endpoint: /auth/redirect
Method: GET
Description: Handles the OAuth2 callback and saves the access token.
  • Get Events
Endpoint: /events
Method: GET
Description: Fetches up to 10 upcoming events from the user's Google Calendar.
  • Create Event
Endpoint: /events
Method: POST
Description: Creates a new event in the user's Google Calendar.

Sample Request:

{
    "summary": "Project Kickoff Meeting",
    "description": "Discussing the new project with the team.",
    "start": {
        "dateTime": "2024-10-15T09:00:00-07:00",
        "timeZone": "America/Los_Angeles"
    },
    "end": {
        "dateTime": "2024-10-15T10:00:00-07:00",
        "timeZone": "America/Los_Angeles"
    },
    "location": "Google Meet",
    "attendees": ["email@example.com"]
}

Open http://127.0.0.1:8000/docs to test endpoints with swagger.

Now it's your time to plug and play, happy coding!

Source Code: Github Repository

0
Subscribe to my newsletter

Read articles from Rasel Hasan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rasel Hasan
Rasel Hasan

Rasel Hasan is a recent CS graduate with a problem solving mindset, currently contributing to real world web engineering projects. He participated in 10+ onsite and 135+ online programming contests including ICPC and NCPC. He solved 1700+ data structures and algorithmic problems. He finds interest in learning and deep diving into web technologies.