use openai_auth::{OAuthClient, OAuthConfig, Result, open_browser, run_callback_server};
#[tokio::main]
async fn main() -> Result<()> {
println!("=== OpenAI OAuth - Automatic with Callback Server ===\n");
let config = OAuthConfig::builder().redirect_port(1455).build();
let client = OAuthClient::new(config)?;
let flow = client.start_flow()?;
println!("🌐 Opening browser and starting callback server...");
let tokens_future = run_callback_server(1455, &flow.state, &client, &flow.pkce_verifier);
match open_browser(&flow.authorization_url) {
Ok(_) => println!("✅ Browser opened! Waiting for authorization..."),
Err(e) => {
println!("⚠️ Could not open browser: {}", e);
println!("Please manually visit: {}", flow.authorization_url);
}
}
println!("\n⏳ Waiting for OAuth callback and token exchange...");
let tokens = tokens_future.await?;
println!("✅ Authentication complete!");
println!("\n✅ Success!");
println!(
"Access token: {}...",
&tokens.access_token[..30.min(tokens.access_token.len())]
);
println!("Expires in: {:?}", tokens.expires_in());
if let Ok(account_id) = client.extract_account_id(&tokens.access_token) {
println!("ChatGPT Account ID: {}", account_id);
}
Ok(())
}