from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request import os import pickle import json # OAuth 2.0 scopes SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"] def get_token_interactive(): """Perform OAuth authentication with user interaction.""" creds = None token_file = "youtube_token.pickle" # Check if token file exists if os.path.exists(token_file): with open(token_file, "rb") as token: creds = pickle.load(token) # If no valid credentials, authenticate user if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: try: creds.refresh(Request()) print("Token refreshed successfully!") except Exception as e: print(f"Token refresh failed: {e}") creds = None if not creds: print("Starting interactive authentication...") flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", SCOPES) creds = flow.run_local_server(port=8080) # Save the credentials to a file with open(token_file, "wb") as token: pickle.dump(creds, token) print(f"Token saved to {token_file}") else: print("Token already exists and is valid.") return creds def get_token_non_interactive(): """Try to use existing saved OAuth token.""" token_file = "youtube_token.pickle" if os.path.exists(token_file): try: with open(token_file, "rb") as token: creds = pickle.load(token) if creds and creds.valid: print("Using existing valid OAuth token.") return creds elif creds and creds.expired and creds.refresh_token: try: creds.refresh(Request()) print("OAuth token refreshed successfully!") # Save refreshed token with open(token_file, "wb") as token: pickle.dump(creds, token) return creds except Exception as e: print(f"OAuth token refresh failed: {e}") except Exception as e: print(f"Error loading saved OAuth token: {e}") print("No valid OAuth token found.") print("Run: python3 auth.py --interactive to authenticate.") return None print("No valid non-interactive OAuth token available.") print("Run: python3 auth.py --interactive to authenticate.") return None def get_token(): """Main authentication function - tries non-interactive first.""" return get_token_non_interactive() if __name__ == "__main__": import sys if "--interactive" in sys.argv: creds = get_token_interactive() else: creds = get_token_non_interactive() if not creds: print("\nTo authenticate interactively, run: python3 auth.py --interactive") if creds: print("Authentication successful!") else: print("Authentication failed!") sys.exit(1)