This project demonstrates how to build a Python Flask API that connects to Google Calendar API. Users can connect their Google calendar with this service, store user tokens in the database, and retrieve calendar events for a specific date range using those tokens.
Before running the application, you need to have a MongoDB instance running. You can create a free instance on MongoDB Atlas.
# Define MongoDB schema
user_schema = {
'user_id': str,
'credentials_data': dict,
'credentials': str
}
- Create a project on the Google Cloud Console.
- Create OAuth2 credentials for the project by following the steps and Enable the Google Calendar API for the project.
- Keep user type external
- You can keep the app domain, and the Authorized domain sections empty
- Click on ADD or remove scopes: and select:
https://www.googleapis.com/auth/calendar
- In Test Users add your email id
- create new credentials
- Select OAuth client ID
- In Applicatio type select web application
- Under Authorized JavaScript origins add:
http://localhost:5000
- Under Authorized redirect URIs add:
http://localhost:5000/callback
- Download the client_secret.json file
- Clone this repository using git clone https://github.com//google-calendar-api.git
- Navigate to the cloned repository
- Build a docker image using
docker build -t meet:1.0.0 .
- Run the docker image using the following command:
docker run -p 5000:5000 \
-e MONGO_URI=<your-mongodb-connection-string> \
-e MONGO_DB=<your-mongodb-database> \
-e MONGO_COLLECTION=<your-mongodb-collection> \
-e GOOGLE_CLIENT_ID=<your-google-client-id> \
-e CLIENT_SECRET=<your-google-client-secret> \
meet:1.0.0
Note: Here MongoDB uri should be url encoded, and if there's a & symbol, it should come in "", also the CLIENT_SECRET recieves the json data of your client_secret.jsom file, wrapped in single inverted commas ''.
- Access the application by visiting http://localhost:5000 on your web browser.
- Clone this repository using git clone https://github.com//google-calendar-api.git
- Navigate to the cloned repository using cd google-calendar-api
- Create a Python virtual environment using python -m venv env
- Activate the environment using source env/bin/activate (Linux/Mac) or env\Scripts\activate (Windows)
- Install the dependencies using pip install -r requirements.txt
- Create a .env file in the app directory with the following environment variables:
GOOGLE_CLIENT_SECRET=<your-google-client-secret>
GOOGLE_CLIENT_ID=<your-google-client-id>
MONGO_URI=<your-mongodb-connection-string>
MONGO_DB=<your-mongodb-database>
MONGO_COLLECTION=<your-mongodb-collection>
CLIENT_SECRET=<your-google-client-secret-json>
Note: Here MongoDB uri should be url encoded, and if there's a & symbol, it should come in "", also the CLIENT_SECRET recieves the json data of your client_secret.jsom file, wrapped in single inverted commas ''.
- Start the application using
flask run
- Access the application by visiting http://localhost:5000 on your web browser.
/: Home page /login: Login using Google OAuth2 /logout: Clears the session /events (GET): Page to select the dates /events (POST): Pass in start_date, end_date, and user_id (Your Google ID)
- Uses Flask-Limiter to limit the number of requests. The /events route is limited to 5 requests per minute for testing purposes.
- Uses refresh_token to automatically refresh the tokens. If the refresh_token is expired, the user is redirected to the sign-in page.
- Index page
- OAuth page
- Events range page
- Events result page | Post request
- Limit handling
- Postman post request
- Docker hosted