Spaces:
Sleeping
Sleeping
File size: 1,605 Bytes
2d5e892 | 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 | from __future__ import annotations
import os
from pathlib import Path
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# REQUIRED scopes based on your plan:
# - read messages, move labels, mark read => modify
# - send mail => send
SCOPES = [
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.send",
]
ROOT = Path(__file__).resolve().parent
CREDS_PATH = Path("backend/credentials.json")
TOKEN_PATH = Path("backend/token.json")
def main() -> None:
if not CREDS_PATH.exists():
raise FileNotFoundError(
f"Missing {CREDS_PATH}. Download OAuth client JSON from Google Cloud and save as credentials.json in this folder."
)
creds: Credentials | None = None
# Load existing token if present
if TOKEN_PATH.exists():
creds = Credentials.from_authorized_user_file(str(TOKEN_PATH), SCOPES)
# Refresh or re-authenticate
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(str(CREDS_PATH), SCOPES)
# Local loopback server OAuth (Desktop app)
creds = flow.run_local_server(port=0)
# Save token
TOKEN_PATH.write_text(creds.to_json(), encoding="utf-8")
print("✅ OAuth complete.")
print(f"Saved token: {TOKEN_PATH}")
print("Scopes granted:", creds.scopes)
if __name__ == "__main__":
main()
|