This repository contains a Django REST API for integrating with Google Calendar. The API provides two endpoints for initializing the Google authentication process and handling the authentication redirect.
It has been deployed at replit
/rest/v1/calendar/init/
: Initializes the Google authentication and provides the authentication URL for OAuth2. Once authenticated, it redirects to the next endpoint/rest/v1/calendar/redirect/
./rest/v1/calendar/redirect/
: Handles the authentication redirect from Google. Retrieves the user's credentials and uses them to access the Google Calendar API. Returns a response containing the user's calendar events.
Before running the API, make sure you have the following prerequisites:
google-auth = "==1.8.0"
google-auth-oauthlib = "==0.4.1"
google-auth-httplib2 = "==0.0.3"
google-api-python-client = "==1.7.2"
django-cors-headers = "*"
django-sslserver = "*"
djangorestframework = "*"
pipenv
- Clone the repository:
git clone https://github.com/ayushjaipuriyar/convin-assessment.git
cd convin-assessment
- Install the dependencies:
pipenv shell
pipenv install
- Configure Google OAuth2:
Obtain the client secret JSON file from the Google Developer Console.
Rename the client secret JSON file to google_oauth_client_secret.json.
Replace settings.GOOGLE_OAUTH_CLIENT_SECRET in GoogleCalendarInitView and GoogleCalendarRedirectView with the path to your google_oauth_client_secret.json file.
- Apply database migrations:
python manage.py makemigrations
python manage.py mmigrate
- Start the development server:
python manage.py runsslserver
The API will be accessible at http://localhost:8000/.
You can interact with the API using your preferred REST client (e.g., cURL, Postman). Here's an example workflow:
But this has api has djangorestframework which allows us to see the results in the preffered web choice
Initialize Google authentication by making a GET request to /rest/v1/calendar/init/. This endpoint will return a response containing the authorization URL.
Redirect the user to the obtained authorization URL. After authentication, the user will be redirected to /rest/v1/calendar/redirect/.
Make a GET request to /rest/v1/calendar/redirect/ to retrieve the user's calendar events. The API will handle the authentication and return a response containing the calendar events.
The code contains two functions, GoogleCalendarInitView and GoogleCalendarRedirectView, which correspond to the two API endpoints.
-
GoogleCalendarInitView: Initializes the Google authentication process. If the user's credentials are already stored in the session, it redirects to the redirect endpoint. Otherwise, it generates the authorization URL and stores the state parameter in the session.
-
GoogleCalendarRedirectView: Handles the authentication redirect from Google. It retrieves the user's credentials, validates them, and uses them to access the Google Calendar API. It then retrieves the user's calendar events and returns them in the response.
- It appears that Replit, does not have proper support for opening of new web pages. This limitation poses a problem when trying to load the authentication URL for the Google consent page. As a result, users are unable to log in and access the API response.
- Google has become increasingly strict about the usage of its APIs, particularly for non-production applications. Previously, developers could authenticate their applications without much restriction. However, to ensure better control and security, Google now requires that only pre-defined testing accounts are allowed to authenticate applications that are not intended for production use.