Files
ewoooc/docs/guides/google_drive_setup.md
2026-06-25 18:36:29 +08:00

48 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Google Drive API 設定指南
## 📋 功能說明
系統自動化流程PChome 後台業績匯出):
1. PChome 後台業績 Excel 先被放入 Google Drive `當日業績匯入` 資料夾。
2. `momo-scheduler` **每 30 分鐘**檢查待匯入檔案。
3. 自動下載 `即時業績_當日.xlsx` 或符合設定 pattern 的 Excel。
4. 自動辨識明細 worksheet / 表頭列,匯入至 `daily_sales_snapshot` 並同步 `realtime_sales_monthly`
5. 成功檔案歸檔至 `已匯入`;格式或日期不合格的檔案移至 `匯入失敗`,避免重複告警。
---
## 🔑 認證資訊庫
| 項目 | 值 |
|-----|-----|
| Google Cloud 專案名 | `wooo-481204` |
| Client ID | `132823079326-h9cvj5eahigm8hp9q0b7t5rk77bhu3gp...` |
| 認證帳號 | `owen.tsai@gmail.com` |
---
## 🛠️ 首次認證步驟
正式排程不可啟動瀏覽器;只有人工在可互動環境重新授權時,才允許進入 OAuth 授權流程,而且仍只印出授權網址。
`config/google_token.json` 遺失或過期,執行以下指令:
```bash
GOOGLE_DRIVE_ALLOW_INTERACTIVE_AUTH=true python3 -c "from services.google_drive_service import drive_service; drive_service.authenticate()"
```
執行後系統會在 terminal 印出 Google 授權網址;請手動複製到可登入 Google 的瀏覽器完成授權。
正式排程與容器內不會自動尋找或啟動本機瀏覽器。
若正式環境仍只有舊版 `config/google_token.pickle`,需在可信任的正式容器中做一次性轉換:
```bash
MOMO_ALLOW_LEGACY_GOOGLE_TOKEN_PICKLE_MIGRATION=true python3 scripts/tools/migrate_google_drive_token.py
```
正式 `config/` 是 scheduler 與 app 共用的 bind mount容器必須能寫入
`config/google_token.json`,否則 token refresh 後無法持久化,主機重啟後會再次失敗。
若正式 Docker 啟用 user namespace remaphost 端需讓 remap 後的 container root 可寫入此目錄。
---
## 📁 資料夾結構要求
Google Drive 根目錄必須存在:
`我的雲端硬碟/業績報表/當日業績/`
> 後續若要把 PChome 後台人工匯出改成全自動,先閱讀
> `docs/guides/pchome_sales_import_automation.md`。