88 lines
No EOL
2.9 KiB
Python
88 lines
No EOL
2.9 KiB
Python
"""
|
|
Test script to verify the new Twitch API authentication
|
|
"""
|
|
import os
|
|
import sys
|
|
import logging
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
|
|
logger = logging.getLogger('auth-test')
|
|
|
|
def test_twitch_auth():
|
|
"""Test the Twitch authentication with client credentials flow"""
|
|
# Check for required libraries
|
|
try:
|
|
import requests
|
|
except ImportError:
|
|
logger.error("The 'requests' library is required. Install it with: pip install requests")
|
|
return False
|
|
|
|
try:
|
|
from src.core.auth import TwitchAuth
|
|
except ImportError:
|
|
logger.error("Failed to import TwitchAuth from src.core.auth")
|
|
return False
|
|
|
|
# Get credentials from environment variables
|
|
client_id = os.environ.get("TWITCH_CLIENT_ID")
|
|
client_secret = os.environ.get("TWITCH_CLIENT_SECRET")
|
|
|
|
if not client_id or not client_secret:
|
|
logger.error("TWITCH_CLIENT_ID and TWITCH_CLIENT_SECRET environment variables must be set")
|
|
logger.info("You can get these from https://dev.twitch.tv/console/apps")
|
|
return False
|
|
|
|
# Try to get a token using the TwitchAuth class
|
|
logger.info("Attempting to get Twitch OAuth token...")
|
|
|
|
try:
|
|
auth = TwitchAuth(client_id, client_secret)
|
|
oauth_token = auth.get_oauth_token()
|
|
|
|
if not oauth_token or not auth.access_token:
|
|
logger.error("Failed to get token using TwitchAuth")
|
|
return False
|
|
|
|
logger.info("Successfully obtained access token!")
|
|
if auth.token_expiry:
|
|
expiry_hours = (auth.token_expiry - time.time()) / 3600
|
|
logger.info(f"Token expires in: {expiry_hours:.1f} hours")
|
|
|
|
# Validate the token by making a simple API call
|
|
logger.info("Validating token with a simple API call...")
|
|
|
|
headers = {
|
|
'Client-ID': client_id,
|
|
'Authorization': f"Bearer {auth.access_token}"
|
|
}
|
|
|
|
# Get top games as a test
|
|
response = requests.get('https://api.twitch.tv/helix/games/top', headers=headers)
|
|
|
|
if response.status_code == 200:
|
|
games = response.json().get('data', [])
|
|
logger.info(f"API call successful! Retrieved {len(games)} top games.")
|
|
if games:
|
|
logger.info(f"Top game: {games[0]['name']}")
|
|
return True
|
|
else:
|
|
logger.error(f"API call failed: {response.status_code} - {response.text}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error during authentication: {e}")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
import time
|
|
print("Testing Twitch API authentication...")
|
|
if test_twitch_auth():
|
|
print("Authentication test PASSED!")
|
|
sys.exit(0)
|
|
else:
|
|
print("Authentication test FAILED!")
|
|
sys.exit(1) |