A simple Python script that allows ChatGPT to read and sync files from Google Drive using a service account.
- 🔐 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
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google Drive API:
- Go to "APIs & Services" > "Library"
- Search for "Google Drive API"
- Click "Enable"
- Create a service account:
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "Service Account"
- Fill in the details and click "Create"
- 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
The service account can only access files that are explicitly shared with it. You need to:
- Share the Google Drive folders/files you want to access with the service account email
- The service account email looks like:
[email protected]
pip install -r requirements.txt
Place your downloaded service account JSON file in the project directory and name it service-account.json
, or update the path in the script.
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)
# 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']}")
You can use this script with ChatGPT by:
- Direct Integration: Include the script in your ChatGPT conversation and ask it to use the functions
- API Wrapper: Create a simple API wrapper around this script
- 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
- 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)
- 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
- Authentication Error: Make sure your service account JSON file is valid and the Google Drive API is enabled
- No Files Found: Ensure you've shared the folders/files with the service account email
- Permission Denied: Check that the service account has access to the specific files
- File Not Found: Verify the file ID is correct
"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
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"
}
This project is open source and available under the MIT License.