""" scripts/get_token.py Génère youtube-token.json via OAuth2 automatique (ouvre le navigateur tout seul). Usage: python scripts/get_token.py """ import os import sys import json from datetime import datetime SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__)) YOUTUBE_DIR = os.path.dirname(SCRIPTS_DIR) TOKEN_PATH = os.path.join(YOUTUBE_DIR, "youtube-token.json") CREDENTIALS_PATH = os.path.join(YOUTUBE_DIR, "client_secret.json") SCOPES = [ "https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube", ] if not os.path.exists(CREDENTIALS_PATH): print(f"\n[X] Fichier manquant : {CREDENTIALS_PATH}") print(" Télécharge-le depuis : https://console.cloud.google.com/apis/credentials") sys.exit(1) # Vérifier token existant if os.path.exists(TOKEN_PATH): with open(TOKEN_PATH, "r") as f: token = json.load(f) expiry_ts = token.get("expiry", token.get("token_expiry", "")) print(f"\n[i] Token existant trouvé : {TOKEN_PATH}") if expiry_ts: print(f" Expire le : {expiry_ts}") answer = input(" Regénérer quand même ? (o/N) : ").strip().lower() if answer != "o": print("[OK] Token conservé. Lance : python youtube_sync.py\n") sys.exit(0) try: from google_auth_oauthlib.flow import InstalledAppFlow except ImportError: print("\n[X] Module manquant. Lance : pip install google-auth-oauthlib") sys.exit(1) print("\n╔══════════════════════════════════════════════════════╗") print("║ DARKMEDIA-X — Autorisation YouTube API ║") print("╚══════════════════════════════════════════════════════╝\n") print("[*] Ouverture du navigateur pour l'autorisation Google...") print(" Si la page ne s'ouvre pas, copie l'URL affichée dans ton navigateur.\n") flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_PATH, SCOPES) # run_local_server ouvre le navigateur et capture le code automatiquement creds = flow.run_local_server( port=0, prompt="consent", access_type="offline", success_message="✅ Autorisation accordée ! Tu peux fermer cet onglet et retourner au terminal.", ) # Sauvegarder au format compatible avec upload.js token_data = { "access_token": creds.token, "refresh_token": creds.refresh_token, "token_uri": creds.token_uri, "client_id": creds.client_id, "client_secret": creds.client_secret, "scopes": list(creds.scopes), "expiry": creds.expiry.isoformat() if creds.expiry else None, "expiry_date": int(creds.expiry.timestamp() * 1000) if creds.expiry else None, } with open(TOKEN_PATH, "w") as f: json.dump(token_data, f, indent=2) print(f"\n[OK] Token sauvegardé : {TOKEN_PATH}") if creds.expiry: print(f" Expire le : {creds.expiry.strftime('%Y-%m-%d %H:%M')}") print("\n[>] Lance maintenant : python youtube_sync.py\n")