Kaynağa Gözat

token refresh

Murad Jabir 1 yıl önce
ebeveyn
işleme
1ebb4a3c29
1 değiştirilmiş dosya ile 9 ekleme ve 2 silme
  1. 9 2
      main.py

+ 9 - 2
main.py

@@ -24,17 +24,24 @@ def update_yt_dlp():
     print("yt-dlp updated successfully!")
 
 def authenticate_with_token():
-    """Authenticate using the saved token."""
+    """Authenticate using the saved token, with automatic refresh."""
     if not os.path.exists(TOKEN_FILE):
         raise FileNotFoundError(f"Token file '{TOKEN_FILE}' not found. Please run the token retrieval script first.")
 
+    # Load credentials from the token file
     with open(TOKEN_FILE, "rb") as token:
         creds = pickle.load(token)
 
-    # Refresh the token if needed
+    # Refresh the token if it has expired
     if creds and creds.expired and creds.refresh_token:
+        print("Access token expired. Refreshing...")
         creds.refresh(Request())
+        # Save the refreshed credentials back to the file
+        with open(TOKEN_FILE, "wb") as token:
+            pickle.dump(creds, token)
+        print("Token refreshed successfully!")
 
+    # Build the YouTube API client
     youtube = build("youtube", "v3", credentials=creds)
     return youtube