99 lines
No EOL
3.5 KiB
Python
99 lines
No EOL
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Setup script for Twitch authentication
|
|
|
|
This script helps users generate a Twitch user access token with proper IRC scopes.
|
|
"""
|
|
import os
|
|
import sys
|
|
import logging
|
|
import argparse
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables if available
|
|
load_dotenv()
|
|
|
|
# Add parent directory to the path so we can import our modules
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
# Import the auth module
|
|
from src.core.auth import TwitchAuth
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
|
|
logger = logging.getLogger('twitch-auth-setup')
|
|
|
|
def setup_twitch_auth(client_id=None, client_secret=None, scopes=None, manual_mode=False, force_new_token=False):
|
|
"""
|
|
Setup Twitch authentication by generating a user access token
|
|
|
|
Args:
|
|
client_id: Twitch client ID
|
|
client_secret: Twitch client secret
|
|
scopes: Space-separated list of scopes
|
|
manual_mode: Whether to use manual mode (no browser)
|
|
force_new_token: Force generation of a new token even if a cached one exists
|
|
|
|
Returns:
|
|
bool: True if successful, False otherwise
|
|
"""
|
|
try:
|
|
# Use provided values, then env vars
|
|
client_id = client_id or os.environ.get('TWITCH_CLIENT_ID')
|
|
client_secret = client_secret or os.environ.get('TWITCH_CLIENT_SECRET')
|
|
|
|
# Default scopes for IRC chat
|
|
default_scopes = "chat:read chat:edit"
|
|
scopes = scopes or os.environ.get('TWITCH_SCOPES', default_scopes)
|
|
|
|
# Define redirect URI
|
|
redirect_uri = os.environ.get('TWITCH_REDIRECT_URI', "https://localhost:3000")
|
|
|
|
# Make sure the cache directory exists
|
|
cache_dir = os.path.join("data", "cache")
|
|
os.makedirs(cache_dir, exist_ok=True)
|
|
|
|
# Create the token cache file path
|
|
token_cache_file = os.path.join(cache_dir, "token_cache.json")
|
|
|
|
# Create auth handler
|
|
auth = TwitchAuth(
|
|
client_id=client_id,
|
|
client_secret=client_secret,
|
|
token_cache_file=token_cache_file,
|
|
redirect_uri=redirect_uri,
|
|
scopes=scopes
|
|
)
|
|
|
|
# Get the token, which will automatically trigger setup if needed
|
|
try:
|
|
oauth_token = auth.get_oauth_token(manual_auth=manual_mode, force_new_token=force_new_token)
|
|
return True
|
|
except ValueError as e:
|
|
logger.error(f"Authentication failed: {e}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error during setup: {e}")
|
|
return False
|
|
|
|
def main():
|
|
"""Main function"""
|
|
parser = argparse.ArgumentParser(description="Setup Twitch authentication")
|
|
parser.add_argument("--client-id", help="Twitch Client ID")
|
|
parser.add_argument("--client-secret", help="Twitch Client Secret")
|
|
parser.add_argument("--scopes", help="Space-separated list of scopes")
|
|
parser.add_argument("--manual", action="store_true", help="Use manual mode (no browser)")
|
|
parser.add_argument("--force-new-token", action="store_true", help="Force generation of a new token even if a cached one exists")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if setup_twitch_auth(args.client_id, args.client_secret, args.scopes, args.manual, args.force_new_token):
|
|
print("Setup completed successfully!")
|
|
sys.exit(0)
|
|
else:
|
|
print("Setup failed!")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |