From 09c7e7adedb0bbd65a0910d63b8a0502da86ed98 Mon Sep 17 00:00:00 2001 From: qiupinhua Date: Fri, 13 Feb 2026 18:37:21 +0800 Subject: [PATCH] feat: change OAuth login command for providers --- README.md | 1 + nanobot/cli/commands.py | 36 ++++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index eb2ff7f..8d0ab4d 100644 --- a/README.md +++ b/README.md @@ -597,6 +597,7 @@ That's it! Environment variables, model prefixing, config matching, and `nanobot | `nanobot agent --logs` | Show runtime logs during chat | | `nanobot gateway` | Start the gateway | | `nanobot status` | Show status | +| `nanobot provider login openai-codex` | OAuth login for providers | | `nanobot channels login` | Link WhatsApp (scan QR) | | `nanobot channels status` | Show channel status | diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 1ee2332..f2a7ee3 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -860,29 +860,37 @@ def status(): # OAuth Login # ============================================================================ +provider_app = typer.Typer(help="Manage providers") +app.add_typer(provider_app, name="provider") -@app.command() -def login( + +@provider_app.command("login") +def provider_login( provider: str = typer.Argument(..., help="OAuth provider to authenticate with (e.g., 'openai-codex')"), ): """Authenticate with an OAuth provider.""" console.print(f"{__logo__} OAuth Login - {provider}\n") - + if provider == "openai-codex": try: - from oauth_cli_kit import get_token as get_codex_token - - console.print("[cyan]Starting OpenAI Codex authentication...[/cyan]") - console.print("A browser window will open for you to authenticate.\n") - - token = get_codex_token() - - if token and token.access: - console.print(f"[green]✓ Successfully authenticated with OpenAI Codex![/green]") - console.print(f"[dim]Account ID: {token.account_id}[/dim]") - else: + from oauth_cli_kit import get_token, login_oauth_interactive + token = None + try: + token = get_token() + except Exception: + token = None + if not (token and token.access): + console.print("[cyan]No valid token found. Starting interactive OAuth login...[/cyan]") + console.print("A browser window may open for you to authenticate.\n") + token = login_oauth_interactive( + print_fn=lambda s: console.print(s), + prompt_fn=lambda s: typer.prompt(s), + ) + if not (token and token.access): console.print("[red]✗ Authentication failed[/red]") raise typer.Exit(1) + console.print("[green]✓ Successfully authenticated with OpenAI Codex![/green]") + console.print(f"[dim]Account ID: {token.account_id}[/dim]") except ImportError: console.print("[red]oauth_cli_kit not installed. Run: pip install oauth-cli-kit[/red]") raise typer.Exit(1)