|
1 | 1 | # auth.py |
2 | | -import json, os, time |
| 2 | +import json, os, time, sys |
3 | 3 | from msal import PublicClientApplication, SerializableTokenCache |
| 4 | +from pathlib import Path |
4 | 5 |
|
5 | | -CFG_PATH = "config.json" |
| 6 | +# Resolve config.json both in dev and in PyInstaller .app bundles |
| 7 | +if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): |
| 8 | + BASE_DIR = Path(sys._MEIPASS) |
| 9 | +else: |
| 10 | + BASE_DIR = Path(__file__).parent |
| 11 | + |
| 12 | +CFG_PATH = str((BASE_DIR / "config.json").resolve()) |
6 | 13 |
|
7 | 14 | def load_config(): |
8 | | - with open(CFG_PATH, "r", encoding="utf-8") as f: |
9 | | - return json.load(f) |
| 15 | + try: |
| 16 | + with open(CFG_PATH, "r", encoding="utf-8") as f: |
| 17 | + return json.load(f) |
| 18 | + except FileNotFoundError: |
| 19 | + raise RuntimeError(f"Missing config.json at {CFG_PATH}. If running as .app, ensure --add-data 'config.json:.' during build.") |
10 | 20 |
|
11 | 21 | cfg = load_config() |
12 | 22 | CLIENT_ID = cfg["client_id"] |
13 | 23 | SCOPES = cfg.get("scopes", ["Files.ReadWrite.All"]) |
14 | | -CACHE_FILE = cfg.get("token_cache_file", "token_cache.bin") |
| 24 | +# 使用 Application Support 路径保存 token 缓存 |
| 25 | +CACHE_FILE = str(Path.home() / "Library/Application Support/OneDriveUploader/token_cache.bin") |
| 26 | +Path(CACHE_FILE).parent.mkdir(parents=True, exist_ok=True) |
15 | 27 |
|
16 | 28 | def _load_cache(): |
17 | 29 | cache = SerializableTokenCache() |
|
0 commit comments