Skip to content

dalledajay-coder/google-oauth-inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google OAuth Integration for Home Inventory

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.

Features

  • 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

Project Structure

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)

Prerequisites

  • 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

Installation

  1. Clone the repository:
git clone <repository-url>
cd google-oauth
  1. Create and activate a virtual environment:
python -m venv .venv

# Windows
.venv\Scripts\activate

# macOS/Linux
source .venv/bin/activate
  1. Install dependencies:

For FastAPI:

pip install fastapi uvicorn authlib
pip install google-auth google-auth-oauthlib google-api-python-client

For Flask:

pip install flask
pip install google-auth google-auth-oauthlib google-api-python-client

Optional (for other Python versions):

pip install python-multipart

Setup

  1. 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.json and place it in the project root
  2. Environment Variables (Optional):

    • Create a .env file for additional configuration if needed
GOOGLE_CLIENT_ID=YOUR_GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET=YOUR_GOOGLE_CLIENT_SECRET
SECRET_KEY=YOUR_SECRET_KEY

⚠️ Important: Never commit credentials.json, token.json, or .env files to Git.

Usage

FastAPI Application

Start the FastAPI server:

python fastapi_app.py

Or using uvicorn:

uvicorn fastapi_app:app --host 0.0.0.0 --port 8000

The API will be available at http://localhost:8000

API Endpoints

  • GET / - Welcome page
  • GET /login - Initiate Google OAuth login flow
  • GET /callback - OAuth callback handler (receives authorization code)
  • GET /logout - Logout and clear session
  • POST /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

Example: Save Inventory Item

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.

Flask Application

Start the Flask server:

python flask_app.py

The application will be available at http://localhost:8000

Routes

  • GET / - Welcome page
  • GET /login - Initiate Google OAuth login flow
  • GET /callback - OAuth callback handler
  • GET /logout - Logout and clear session

Utility Functions (main.py)

The main.py file provides reusable functions:

  • authenticate_google() - Authenticate with Google OAuth
  • create_google_sheet(creds) - Create a new Google Sheet
  • save_data_to_sheet(creds, spreadsheet_id, data) - Append data to a sheet
  • upload_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 Sheets
  • https://www.googleapis.com/auth/drive.file - Access to files created by the app

Quickstart Script

Run the standalone script to create a new Google Sheet:

python quickstart.py

This script will:

  1. Authenticate with Google (opens browser for first-time auth)
  2. Create a new spreadsheet named "New Inventory Spreadsheet"
  3. Add a header row with columns: "Item", "Quantity", "Price"
  4. Save credentials to token.json for future use

FastAPI Features

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

Flask Features

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

Security Notes

⚠️ Development Mode: Both applications use 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_TRANSPORT environment variable

Troubleshooting

  1. OAuth Error: "Invalid state"

    • Clear browser cookies and try again
    • Ensure redirect URI in Google Cloud Console matches exactly
  2. Import Errors

    • Activate your virtual environment
    • Reinstall dependencies: pip install -r requirements.txt (if available)
  3. Permission Denied Errors

    • Verify Google Sheets API is enabled in Google Cloud Console
    • Check that OAuth scopes are correctly configured
  4. Token Expired

    • Delete token.json file
    • Re-authenticate by running the application again
  5. "credentials.json not found"

    • Download OAuth credentials from Google Cloud Console
    • Place credentials.json in the project root directory

API Scopes

The application requests the following Google API scopes:

  • https://www.googleapis.com/auth/spreadsheets - Full access to Google Sheets
  • https://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)

License

This project is part of a home inventory chatbot system.

About

Google OAuth 2.0 integration with Google Sheets API for home inventory management

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages