""" setup_google.py - One-time Google API setup for Job Automation Agent. Run: python setup_google.py """ import os, sys, json, webbrowser from pathlib import Path def main(): print() print("=" * 58) print(" Google Sheets + Drive Setup - Job Automation Agent") print("=" * 58) print() print("This connects the agent to your Google Sheet:") print(" https://docs.google.com/spreadsheets/d/1Ehxt3eortehbtySdtgcSrMhCqmxIMUAmvRqSkII0HJk") print() print("TWO OPTIONS:") print(" A [RECOMMENDED] Service Account - no browser needed, best for automation") print(" B OAuth2 - browser login once, simpler setup") print() choice = input("Choose (A/B): ").strip().upper() if choice == "A": setup_service_account() elif choice == "B": setup_oauth() else: print("Invalid. Run again and choose A or B.") def setup_service_account(): print() print("-" * 56) print(" OPTION A: Service Account Setup") print("-" * 56) print() print("Steps:") print(" 1. Go to: https://console.cloud.google.com") print(" 2. Create or select a project") print(" 3. Enable 'Google Sheets API' and 'Google Drive API'") print(" 4. Go to IAM & Admin -> Service Accounts") print(" 5. Create a new Service Account (any name)") print(" 6. Click the account -> Keys tab -> Add Key -> JSON") print(" 7. Download the JSON file") print(" 8. Rename it to: google_credentials.json") print(f" 9. Place it here: {os.path.abspath('.')}") print(" 10. Share your Google Sheet with the service account email") print(" (the client_email field in the JSON)") print() webbrowser.open("https://console.cloud.google.com/iam-admin/serviceaccounts") input("Press Enter after placing google_credentials.json here...") if Path("google_credentials.json").exists(): with open("google_credentials.json") as f: data = json.load(f) email = data.get("client_email", "") print() print(" Credentials file found!") print(f" Service account email: {email}") print() print(" IMPORTANT - Share your Google Sheet with this email as Editor:") sheet_id = os.getenv("GOOGLE_SHEET_ID", "1Ehxt3eortehbtySdtgcSrMhCqmxIMUAmvRqSkII0HJk") print(f" Sheet: https://docs.google.com/spreadsheets/d/{sheet_id}/edit") print(f" -> Click Share -> Add '{email}' as Editor") print() input("Press Enter after sharing the sheet...") test_connection() else: print("ERROR: google_credentials.json not found. Please try again.") def setup_oauth(): print() print("-" * 56) print(" OPTION B: OAuth2 Setup") print("-" * 56) print() print("Steps:") print(" 1. Go to: https://console.cloud.google.com") print(" 2. Create or select a project") print(" 3. Enable 'Google Sheets API' and 'Google Drive API'") print(" 4. Go to APIs & Services -> Credentials") print(" 5. Create OAuth 2.0 Client ID -> Desktop App") print(" 6. Download JSON -> rename to: google_oauth_client.json") print(f" 7. Place it here: {os.path.abspath('.')}") print() webbrowser.open("https://console.cloud.google.com/apis/credentials") input("Press Enter after placing google_oauth_client.json here...") if Path("google_oauth_client.json").exists(): print(" OAuth client file found! Starting browser login...") test_connection() else: print("ERROR: google_oauth_client.json not found. Please try again.") def test_connection(): print() print("Testing Google connection...") try: from src.gsheets import _get_client, get_sheet_url from dotenv import load_dotenv load_dotenv() sheet_id = os.getenv("GOOGLE_SHEET_ID", "1Ehxt3eortehbtySdtgcSrMhCqmxIMUAmvRqSkII0HJk") client = _get_client() sh = client.open_by_key(sheet_id) print(f" Connected to Google Sheets!") print(f" Sheet name: {sh.title}") print(f" Sheet URL: {get_sheet_url(sheet_id)}") print() print("Setup complete! Run python main.py to start the agent.") except Exception as e: print(f" Connection failed: {e}") print() print("Troubleshooting:") print(" - Ensure credentials file is correct") print(" - Ensure Google Sheets API + Drive API are enabled") print(" - For service accounts: share the sheet with the service account email") if __name__ == "__main__": from dotenv import load_dotenv load_dotenv() main()