Integrating Google Auth and Websocket
https://charlie-backend.duckdns.org/ping
https://www.youtube.com/watch?v=e8AJoxCIfMQ
This Django project implements integration with Google OAuth 2.0, Google Drive, and real-time WebSocket communication. It provides endpoints for authenticating users through Google, interacting with Google Drive (upload, download, and list files), and enabling real-time chat between users.
-
Google OAuth 2.0 Authentication
- Authenticate users with Google OAuth 2.0
- Securely store and manage user tokens
-
Google Drive Integration
- Connect to Google Drive
- Upload files to Google Drive
- Download files from Google Drive
- List files from Google Drive
- Use Google Picker API for file selection
-
Real-time Chat
- WebSocket-based real-time chat between users
- Chat history persistence
- Real-time message delivery
- Python 3.8+
- Django 4.2+
- PostgreSQL (Neon PostgreSQL for production)
- Google Cloud Platform account with OAuth credentials
git clone https://github.com/nullfox-coder/django_charlie.git
cd charlie_backend
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
Create a .env
file in the project root with the following variables:
DEBUG=True
SECRET_KEY=your-secret-key
ALLOWED_HOSTS=localhost,127.0.0.1
DATABASE_URL=your-postgresql-connection-string
client_secrets=your-google-client-secrets
ENCRYPTION_KEY=your-key
If using PostgreSQL with Neon:
# The migrations will automatically apply using the DATABASE_URL
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 8080
- Go to the Google Cloud Console
- Create a new project
- Enable the Google Drive API, Google Picker API, and People API
- Create OAuth credentials (Web application type)
- Set the authorized redirect URI to match your GOOGLE_REDIRECT_URI env variable
- Note your client ID, client secret, and API key
-
Initiate Google Auth:
GET /auth/google/init/
- Initiates the Google OAuth flow
- Response: Redirect URL to Google's authentication page
-
Google Auth Callback:
GET /auth/google/callback/
- Callback URL for Google authentication
- Query parameters:
code
: Authorization code provided by Googlestate
: State parameter for security
- Response: User authentication data including access token
-
Drive Auth Status:
GET /drive/auth/
- Checks if user has connected Google Drive
- Response: Connection status and user details
-
List Drive Files:
GET /drive/files/
- Lists files from user's Google Drive
- Response: Array of file objects with metadata
-
Upload File:
POST /drive/upload/
- Uploads a file to user's Google Drive
- Request:
multipart/form-data
with file attachment - Response: File metadata of the uploaded file
-
Download File:
GET /drive/download/{file_id}/
- Downloads a file from user's Google Drive
- Path parameter:
file_id
- Google Drive file ID - Response: File content as attachment
-
Chat History:
GET /chat/history/{user_id}/
- Gets chat history between current user and specified user
- Path parameter:
user_id
- User ID to get chat history with - Response: Array of message objects with metadata
-
WebSocket Connection:
ws://{host}/ws/chat/{room_name}/?token=your-token
- Establishes WebSocket connection for real-time chat
- Path parameter:
room_name
- Unique identifier for chat room
Import the included Postman collection to test all API endpoints. The collection includes:
- Authentication requests
- Google Drive operations
- Chat functionality
- Environment variables for easy configuration