Spaces:
Sleeping
Sleeping
File size: 4,650 Bytes
7ff6662 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | """
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()
|