#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Google Drive 首次認證腳本 """ import sys import os # 確保可以導入專案模組 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) print("=" * 60) print("Google Drive OAuth 2.0 認證") print("=" * 60) print() print("請確認以下檔案存在:") print(" 📁 config/google_credentials.json") print() # 檢查憑證檔案 if not os.path.exists('config/google_credentials.json'): print("❌ 找不到憑證檔案!") print() print("請依照以下步驟設定:") print("1. 前往 Google Cloud Console 建立 OAuth 2.0 憑證") print("2. 下載 JSON 檔案") print("3. 重新命名為 google_credentials.json") print("4. 放到 config/ 目錄中") print() print("詳細說明請參考: GOOGLE_DRIVE_SETUP.md") sys.exit(1) print("✅ 找到憑證檔案") print() print("正在啟動 OAuth 2.0 認證流程...") print("瀏覽器將自動開啟,請完成授權。") print() try: from services.google_drive_service import drive_service if drive_service.authenticate(): print() print("=" * 60) print("✅ 認證成功!") print("=" * 60) print() print("Token 已儲存至: config/google_token.pickle") print() print("現在可以:") print(" 1. 在網頁介面測試連接: http://localhost/auto_import") print(" 2. 執行測試腳本: python3 test_google_drive.py") print() else: print() print("❌ 認證失敗") print("請檢查憑證檔案是否正確") sys.exit(1) except Exception as e: print() print(f"❌ 認證過程發生錯誤: {str(e)}") import traceback traceback.print_exc() sys.exit(1)