A Python application that provides Google OAuth 2.0 authentication and Google Sheets API integration for managing home inventory items. Includes both FastAPI and Flask implementations.
- Google OAuth 2.0 authentication flow
- Google Sheets API integration for inventory management
- Create and manage inventory spreadsheets
- Save inventory items to Google Sheets
- List user's spreadsheets
- Both FastAPI and Flask web frameworks supported
google-oauth/
├── fastapi_app.py # FastAPI web application with OAuth and Sheets API
├── flask_app.py # Flask web application with OAuth
├── main.py # Core utility functions for Google Sheets operations
├── quickstart.py # Standalone script to create a new Google Sheet
├── credentials.json # Google OAuth credentials (not in Git)
└── .env # Environment variables (not in Git)
- Python 3.12 or higher
- Google Cloud Platform account
- OAuth 2.0 credentials from Google Cloud Console
- Google Sheets API enabled in your GCP project
- Clone the repository:
git clone <repository-url>
cd google-oauth- Create and activate a virtual environment:
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate- Install dependencies:
For FastAPI:
pip install fastapi uvicorn authlib
pip install google-auth google-auth-oauthlib google-api-python-clientFor Flask:
pip install flask
pip install google-auth google-auth-oauthlib google-api-python-clientOptional (for other Python versions):
pip install python-multipart-
Get Google OAuth Credentials:
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google Sheets API
- Navigate to "Credentials" → "Create Credentials" → "OAuth client ID"
- Choose "Web application" as the application type
- Add authorized redirect URIs:
http://localhost:8000/callback(for FastAPI)http://localhost:8000/callback(for Flask)
- Download the credentials JSON file
- Rename it to
credentials.jsonand place it in the project root
-
Environment Variables (Optional):
- Create a
.envfile for additional configuration if needed
- Create a
GOOGLE_CLIENT_ID=YOUR_GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET=YOUR_GOOGLE_CLIENT_SECRET
SECRET_KEY=YOUR_SECRET_KEY
credentials.json, token.json, or .env files to Git.
Start the FastAPI server:
python fastapi_app.pyOr using uvicorn:
uvicorn fastapi_app:app --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
GET /- Welcome pageGET /login- Initiate Google OAuth login flowGET /callback- OAuth callback handler (receives authorization code)GET /logout- Logout and clear sessionPOST /inventory_items- Save inventory item to Google Sheets- Requires authentication
- Request body:
{"name": "string", "category": "string", "quantity": number}
GET /list_spreadsheets- List all user's Google Sheets- Requires authentication
curl -X POST http://localhost:8000/inventory_items \
-H "Content-Type: application/json" \
-d '{"name": "Laptop", "category": "Electronics", "quantity": 1}'Note: You must authenticate first by visiting /login in your browser.
Start the Flask server:
python flask_app.pyThe application will be available at http://localhost:8000
GET /- Welcome pageGET /login- Initiate Google OAuth login flowGET /callback- OAuth callback handlerGET /logout- Logout and clear session
The main.py file provides reusable functions:
authenticate_google()- Authenticate with Google OAuthcreate_google_sheet(creds)- Create a new Google Sheetsave_data_to_sheet(creds, spreadsheet_id, data)- Append data to a sheetupload_image_to_drive(creds, file_name, file_path)- Upload image to Google Drive
Scopes used:
https://www.googleapis.com/auth/spreadsheets- Read/write access to Google Sheetshttps://www.googleapis.com/auth/drive.file- Access to files created by the app
Run the standalone script to create a new Google Sheet:
python quickstart.pyThis script will:
- Authenticate with Google (opens browser for first-time auth)
- Create a new spreadsheet named "New Inventory Spreadsheet"
- Add a header row with columns: "Item", "Quantity", "Price"
- Save credentials to
token.jsonfor future use
The FastAPI application includes:
- Session Management: Uses Starlette SessionMiddleware for storing OAuth credentials
- Spreadsheet Management: Automatically creates "Inventory Items Sheet" spreadsheet
- Sheet Operations:
- Creates spreadsheet with "original_inventory_items_sheet" as default sheet
- Functions to find or create inventory sheets
- Add custom sheets to existing spreadsheets
- Drive Integration: List all user's spreadsheets via Google Drive API
The Flask application provides:
- Simple OAuth Flow: Basic authentication with Google
- Session Storage: Stores credentials in Flask session
- State Verification: Validates OAuth state parameter for security
OAUTHLIB_INSECURE_TRANSPORT=1 to allow HTTP connections for localhost OAuth flows. Do not use this in production.
Production Recommendations:
- Use HTTPS for OAuth redirect URIs
- Store session secret keys securely (use environment variables)
- Implement proper session management
- Use secure cookie settings
- Remove
OAUTHLIB_INSECURE_TRANSPORTenvironment variable
-
OAuth Error: "Invalid state"
- Clear browser cookies and try again
- Ensure redirect URI in Google Cloud Console matches exactly
-
Import Errors
- Activate your virtual environment
- Reinstall dependencies:
pip install -r requirements.txt(if available)
-
Permission Denied Errors
- Verify Google Sheets API is enabled in Google Cloud Console
- Check that OAuth scopes are correctly configured
-
Token Expired
- Delete
token.jsonfile - Re-authenticate by running the application again
- Delete
-
"credentials.json not found"
- Download OAuth credentials from Google Cloud Console
- Place
credentials.jsonin the project root directory
The application requests the following Google API scopes:
https://www.googleapis.com/auth/spreadsheets- Full access to Google Sheetshttps://www.googleapis.com/auth/drive.file- Access to files created by the app (main.py)https://www.googleapis.com/auth/drive.readonly- Read-only access to Google Drive (FastAPI list_spreadsheets)
This project is part of a home inventory chatbot system.