Skip to content

JuanAnsio/gpt-googledrive-reader

Repository files navigation

Google Drive Reader for ChatGPT

A simple Python script that allows ChatGPT to read and sync files from Google Drive using a service account.

Features

  • 🔐 Service Account Authentication - Secure access using Google service account
  • 📁 File Listing - List all files or files in specific folders
  • 🔍 File Search - Search for files by name and type
  • 📖 File Reading - Read content from various file types including:
    • Google Docs (exported as text)
    • Google Sheets (exported as CSV)
    • Google Slides (exported as text)
    • PDFs, text files, and other document types
  • 📊 File Metadata - Get detailed information about files

Setup

1. Create a Google Service Account

  1. Go to the Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Google Drive API:
    • Go to "APIs & Services" > "Library"
    • Search for "Google Drive API"
    • Click "Enable"
  4. Create a service account:
    • Go to "APIs & Services" > "Credentials"
    • Click "Create Credentials" > "Service Account"
    • Fill in the details and click "Create"
  5. Generate a JSON key:
    • Click on your service account
    • Go to "Keys" tab
    • Click "Add Key" > "Create New Key"
    • Choose JSON format and download the file

2. Share Google Drive Folders

The service account can only access files that are explicitly shared with it. You need to:

  1. Share the Google Drive folders/files you want to access with the service account email
  2. The service account email looks like: [email protected]

3. Install Dependencies

pip install -r requirements.txt

4. Place Service Account File

Place your downloaded service account JSON file in the project directory and name it service-account.json, or update the path in the script.

Usage

Basic Usage

from google_drive_reader import GoogleDriveReader

# Initialize the reader
reader = GoogleDriveReader("service-account.json")

# List all files
files = reader.list_files()
for file in files:
    print(f"- {file['name']} ({file['id']})")

# Read a specific file
content = reader.read_file("your-file-id-here")
print(content)

Advanced Usage

# Search for specific files
pdf_files = reader.search_files("report", file_type="pdf")

# List files in a specific folder
folder_files = reader.list_files(folder_id="your-folder-id")

# Get detailed file information
file_info = reader.get_file_info("your-file-id")
print(f"File: {file_info['name']}")
print(f"Size: {file_info['size']} bytes")
print(f"Modified: {file_info['modifiedTime']}")

ChatGPT Integration

You can use this script with ChatGPT by:

  1. Direct Integration: Include the script in your ChatGPT conversation and ask it to use the functions
  2. API Wrapper: Create a simple API wrapper around this script
  3. Function Calling: Use the functions as tools in ChatGPT's function calling feature

Example ChatGPT prompt:

I have a Google Drive reader script. Can you help me:
1. List all PDF files in my drive
2. Read the content of a specific file with ID "abc123"
3. Search for files containing "report" in the name

File Types Supported

  • Google Docs - Exported as plain text
  • Google Sheets - Exported as CSV
  • Google Slides - Exported as plain text
  • PDFs - Read as text (requires text extraction)
  • Text files - Read directly
  • Word documents - Read directly
  • Other files - Downloaded as binary (may need additional processing)

Security Notes

  • Keep your service account JSON file secure and never commit it to version control
  • The service account has read-only access to files shared with it
  • Only share the specific folders/files you want ChatGPT to access
  • Consider using environment variables for the service account path in production

Troubleshooting

Common Issues

  1. Authentication Error: Make sure your service account JSON file is valid and the Google Drive API is enabled
  2. No Files Found: Ensure you've shared the folders/files with the service account email
  3. Permission Denied: Check that the service account has access to the specific files
  4. File Not Found: Verify the file ID is correct

Error Messages

  • "Authentication failed" - Check your service account JSON file
  • "Failed to list files" - Check API permissions and shared access
  • "Failed to read file" - File might not be accessible or in an unsupported format

Example Service Account JSON Structure

Your service account JSON should look like this (with your actual values):

{
  "type": "service_account",
  "project_id": "your-project-id",
  "private_key_id": "your-private-key-id",
  "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "your-client-id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com"
}

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published