import os
import json
from google_auth_oauthlib.flow import InstalledAppFlow

# Scopes required for uploading videos to YouTube
SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CLIENT_SECRETS = os.path.join(SCRIPT_DIR, "client_secrets.json")
TOKEN_FILE = os.path.join(SCRIPT_DIR, "token.json")

# The code provided by the user
AUTH_CODE = "4/0AfrIepC-7_yJTzbqEq1CZ2P-BIdkP_R2CRhvgSNliIgU65rvl687a4ugtJfBkHfnK4sISg"
# The redirect URI used in the request
REDIRECT_URI = "http://localhost:57991/"

def main():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS, SCOPES)
    flow.redirect_uri = REDIRECT_URI
    
    print(f"Exchanging code for token...")
    flow.fetch_token(code=AUTH_CODE)
    
    creds = flow.credentials
    with open(TOKEN_FILE, "w") as token:
        token.write(creds.to_json())
    
    print(f"✅ Successfully saved token to {TOKEN_FILE}")

if __name__ == "__main__":
    main()
