File size: 806 Bytes
41f17f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# backend/gcal.py
import os
from pathlib import Path
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

# Same scope you used everywhere
SCOPES = ["https://www.googleapis.com/auth/calendar.events"]

# HF Spaces persistent disk path (Settings → Persistent storage)
TOKEN_FILE = Path(os.getenv("GCAL_TOKEN_PATH", "/data/google_token.json"))

def get_gcal_service():
    """
    Load saved Google OAuth credentials and return a Calendar API client.
    Raise a clear error if not connected yet.
    """
    if not TOKEN_FILE.exists():
        raise RuntimeError("Google Calendar not connected. Visit /oauth/google/start to connect.")
    creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), SCOPES)
    return build("calendar", "v3", credentials=creds)