# Minecraft Launcher Token Extraction Workflow

## Why This Instead of Playwright?

The Playwright browser automation approach tries to automate Microsoft's interactive login page. This is fragile because:
- Microsoft shows FIDO/Windows Hello pages that can't be automated in headless browsers
- Microsoft's auth flow changes frequently
- Browser automation is slow (~60s per account)

**Token extraction** bypasses all of this by reading tokens the Minecraft Launcher already obtained.

---

## Setup (One-Time on Your Desktop)

### Step 1: Log into Minecraft Launcher

1. Open Minecraft Launcher (Essentials edition) on your desktop
2. Click "Add account" or "Sign in" for each account you want to use
3. Log in normally (this is where FIDO is fine — you're doing it interactively)
4. **Close the launcher** (ensures tokens are written to disk)

### Step 2: Find Your Token File

The launcher stores tokens in plain JSON:

| OS | Path |
|----|------|
| Windows | `%APPDATA%\.minecraft\launcher_accounts.json` |
| Windows (new) | `%LOCALAPPDATA%\Packages\Microsoft.4297127D64EC6_8wekyb3d8bbwe\LocalState\games\com.mojang\minecraftService\accounts.json` |
| Linux | `~/.minecraft/launcher_accounts.json` |
| macOS | `~/Library/Application Support/minecraft/launcher_accounts.json` |

### Step 3: Extract Tokens

Copy `launcher_token_extractor.py` to your desktop machine and run:

```bash
# Auto-detect (tries all known paths)
python launcher_token_extractor.py extract --output tokens.json

# Or specify the file directly
python launcher_token_extractor.py extract --input ~/.minecraft/launcher_accounts.json --output tokens.json
```

### Step 4: Transfer to Sniper Server

```bash
scp tokens.json bub@your-server:/home/bub/minecraft-sniper-gui/tokens.json
```

---

## Using Tokens with the Sniper

The sniper reads `tokens.json` and uses the `mc_access_token` directly:

```bash
python launcher_token_extractor.py status --input tokens.json
# Shows which tokens are valid/expired
```

---

## Refreshing Tokens

Tokens expire. Use the refresh command to get new ones without re-logging in:

```bash
# On the server (tokens have Microsoft refresh tokens embedded)
python launcher_token_extractor.py refresh --input tokens.json --output tokens.json
```

This calls Microsoft's refresh endpoint (simple HTTP POST, no browser) to get new access tokens. **Microsoft refresh tokens last months**, so you only need to re-extract from the launcher occasionally.

When the refresh token itself expires, just re-do Step 1 (re-login in launcher) and Step 3 (re-extract).

---

## Cron Job for Auto-Refresh (Optional)

```bash
# Add to crontab - refresh tokens every 6 hours
0 */6 * * * cd /home/bub/minecraft-sniper-gui && python launcher_token_extractor.py refresh --input tokens.json --output tokens.json 2>&1 | tee -a /var/log/token_refresh.log
```

---

## Token File Format

```json
[
  {
    "email": "user@gmail.com",
    "microsoft_access_token": "eyJ...",
    "microsoft_refresh_token": "0.AQ...",
    "mc_access_token": "eyJ...",
    "mc_uuid": "PlayerName",
    "gamertag": "Player123",
    "xuid": "2539xxxx...",
    "mc_token_expiry": "2026-05-16T21:00:00Z",
    "ms_refresh_expiry": "2026-06-16T20:00:00Z",
  }
]
```

The sniper can read `mc_access_token` directly for API calls, and use `microsoft_refresh_token` for refreshing when needed.
