import os import json try: from google_auth_oauthlib.flow import InstalledAppFlow except ImportError: print("Vui lòng cài đặt thư viện trước bằng lệnh:") print("pip install google-auth-oauthlib") exit(1) SCOPES = ['https://www.googleapis.com/auth/drive'] def main(): print("=== Lấy Google Drive Refresh Token ===") print("1. Trong Google Cloud Console > APIs & Services > Credentials") print("2. Bấm 'Create Credentials' > 'OAuth client ID'") print("3. Application type chọn 'Desktop app', đặt tên bất kỳ") print("4. Tải file JSON về máy, đổi tên thành 'client_secret.json'") print("5. Đặt file 'client_secret.json' vào cùng thư mục với script này") print("---------------------------------------") if not os.path.exists('client_secret.json'): print("\n[!] LỖI: Không tìm thấy file 'client_secret.json' ở thư mục hiện tại!") return print("\nĐang mở trình duyệt để xác thực, vui lòng chờ...") try: flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES) creds = flow.run_local_server(port=0) except Exception as e: print(f"\n[!] LỖI KHI XÁC THỰC: {e}") return if not creds.refresh_token: print("\n[!] LỖI: Không nhận được Refresh Token.") print("Điều này thường xảy ra nếu bạn đã cấp quyền cho app này trước đó.") print("Cách sửa: Vào trang https://myaccount.google.com/permissions") print("Xóa quyền của ứng dụng, sau đó chạy lại script này.") return output = { "client_id": creds.client_id, "client_secret": creds.client_secret, "refresh_token": creds.refresh_token, "token_uri": creds.token_uri } print("\n\n" + "="*50) print(" THÀNH CÔNG") print("="*50) print("\nHÃY COPY TOÀN BỘ ĐOẠN JSON TRONG KHUNG DƯỚI ĐÂY") print("Và dán thay thế vào biến GOOGLE_CREDENTIALS_JSON trên HuggingFace:\n") print("-" * 50) print(json.dumps(output, indent=2)) print("-" * 50) print("\nSau khi cập nhật trên HuggingFace, hãy vào Vercel Restart lại hoặc App sẽ tự động nhận sau vài phút.") if __name__ == '__main__': main()