import requests
import json
import re
from urllib.parse import urlparse, parse_qs

session = requests.Session()
session.headers.update({
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
})

# Step 1: Initiate OAuth flow
print("[1] Initiating OAuth flow...")
resp = session.get(
    'https://oauth.deriv.com/oauth2/authorize',
    params={'app_id': '1', 'scope': 'read', 'redirect_uri': 'https://home.deriv.com'},
    allow_redirects=False
)
print(f"OAuth start: {resp.status_code} -> {resp.headers.get('location','')[:100]}")

# Follow to dashboard login
location = resp.headers.get('location', '')
if 'login' in location:
    resp2 = session.get(location, allow_redirects=False)
    print(f"Login page: {resp2.status_code}")
    
    # Look for ORY flow URL in redirect
    loc2 = resp2.headers.get('location', '')
    print(f"Login redirect: {loc2[:200]}")

