google-oauth-setup.py
One-time Google OAuth setup granting Gmail + Calendar access for the assistant.
Note
Live script from my home-lab server. Tokens, IDs, phone numbers and other secrets have been replaced with
placeholders like
<WHATSAPP_GROUP_ID> — everything else is the real, running code.
#!/usr/bin/env python3
"""One-time Google OAuth setup for Gmail + Calendar access.
Run this on the SERVER after placing credentials.json there.
"""
import json
import os
import sys
CREDS_FILE = "/home/lanky/secrets/google-credentials.json"
TOKEN_FILE = "/home/lanky/secrets/google-token.json"
SCOPES = [
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.events",
]
try:
from google_auth_oauthlib.flow import InstalledAppFlow
except ImportError:
print("ERROR: google-auth-oauthlib not installed.")
sys.exit(1)
if not os.path.exists(CREDS_FILE):
print(f"ERROR: {CREDS_FILE} not found.")
print("Please complete the Google Cloud Console setup first.")
sys.exit(1)
print("Starting Google OAuth flow...")
print("A URL will be printed. Open it in your browser on any device.")
print()
flow = InstalledAppFlow.from_client_secrets_file(CREDS_FILE, SCOPES)
# Use console flow (no local server needed)
flow.run_local_server = None
creds = flow.run_console()
os.makedirs(os.path.dirname(TOKEN_FILE), exist_ok=True)
with open(TOKEN_FILE, "w") as f:
f.write(creds.to_json())
os.chmod(TOKEN_FILE, 0o600)
print(f"\nSuccess! Token saved to {TOKEN_FILE}")
print("The personal assistant can now access Gmail and Google Calendar.")