maya-voice-agent / scripts /authorize_google_calendar.py
rudyByte
feat(E3): Google Calendar Real-Time Booking via Groq Function Calling
2e4c13e
"""
One-time OAuth authorization for Google Calendar access.
Run this ONCE to authorize Maya to access Google Calendar.
"""
import os
import sys
import json
from pathlib import Path
SCOPES = ['https://www.googleapis.com/auth/calendar']
CREDENTIALS_FILE = Path("credentials/google_oauth_credentials.json")
TOKEN_FILE = Path("credentials/google_token.json")
def authorize():
print("\n[LOCK] Google Calendar Authorization for Maya")
print("=" * 50)
if not CREDENTIALS_FILE.exists():
print(f"\n[X] Credentials file not found: {CREDENTIALS_FILE}")
sys.exit(1)
from google_auth_oauthlib.flow import InstalledAppFlow
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
creds = None
# Check for existing valid token
if TOKEN_FILE.exists():
creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), SCOPES)
if creds and creds.valid:
print("\n[OK] Already authorized! Token is valid.")
_print_calendar_info(creds)
return
if creds and creds.expired and creds.refresh_token:
print("\n[REFRESH] Token expired -- refreshing...")
creds.refresh(Request())
_save_token(creds)
print("[OK] Token refreshed successfully.")
return
# Need fresh authorization
print("\n[LIST] Starting OAuth authorization flow...")
print(" This will open your browser.")
print(" Log in with: rudybyte.code@gmail.com\n")
flow = InstalledAppFlow.from_client_secrets_file(
str(CREDENTIALS_FILE), SCOPES
)
try:
# Use a specific port to match our redirect URI
creds = flow.run_local_server(port=8090, open_browser=True)
except Exception as e:
print(f"\n[X] Error: {e}")
print("\n[!] Try running this command in your local terminal manually if the browser doesn't open.")
creds = flow.run_local_server(port=8090, open_browser=False)
_save_token(creds)
print("\n[OK] Authorization successful!")
print(f" Token saved to: {TOKEN_FILE}")
print("\n IMPORTANT: I will now save these to your .env file.")
_print_calendar_info(creds)
def _save_token(creds):
TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)
with open(TOKEN_FILE, 'w') as f:
f.write(creds.to_json())
def _print_calendar_info(creds):
try:
from googleapiclient.discovery import build
service = build('calendar', 'v3', credentials=creds)
cal = service.calendarList().get(calendarId='primary').execute()
print(f"\n [CAL] Calendar authorized for: {cal.get('summary', 'Unknown')}")
except Exception as e:
print(f"\n (Could not fetch calendar info: {e})")
if __name__ == "__main__":
authorize()