1use baidu_netdisk_sdk::BaiduNetDiskClient;
2use std::time::Duration;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();
7
8 println!("=== Baidu NetDisk Auth Flow Example ===");
9 println!();
10
11 let client = BaiduNetDiskClient::builder()
12 .timeout(Duration::from_secs(30))
13 .build()?;
14
15 println!("Client created successfully");
16 println!();
17
18 println!("Step 1: Getting device code...");
19 let device_code = client.authorize().get_device_code().await?;
20
21 println!("Device Code Info:");
22 println!(" User Code: {}", device_code.user_code);
23 println!(" Verification URL: {}", device_code.verification_url);
24 println!(" QR Code URL: {}", device_code.qrcode_url);
25 println!(" Interval: {} seconds", device_code.interval);
26 println!(
27 " Expires in: {} seconds",
28 device_code.expires_at - chrono::Utc::now().timestamp() as u64
29 );
30 println!();
31
32 println!("Please visit the verification URL above and enter the user code to authorize.");
33 println!("Waiting for authorization...");
34 println!();
35
36 let max_attempts = 30;
37 let mut attempts = 0;
38
39 let access_token = loop {
40 attempts += 1;
41
42 if attempts > max_attempts {
43 return Err("Max attempts reached, authorization timeout".into());
44 }
45
46 println!("Attempt {}/{}", attempts, max_attempts);
47
48 match client.authorize().request_access_token(&device_code).await {
49 Ok(Some(token)) => {
50 println!();
51 println!("✅ Authorization successful!");
52 break token;
53 }
54 Ok(None) => {
55 println!(" Authorization pending, waiting...");
56 tokio::time::sleep(Duration::from_secs(device_code.interval as u64)).await;
57 continue;
58 }
59 Err(e) => {
60 println!(" Error: {}", e);
61 tokio::time::sleep(Duration::from_secs(device_code.interval as u64)).await;
62 continue;
63 }
64 }
65 };
66
67 println!();
68 println!("=== Access Token Info ===");
69 println!("Access Token: {}", access_token.access_token);
70 println!("Expires in: {} seconds", access_token.expires_in);
71 println!("Refresh Token: {}", access_token.refresh_token);
72 println!("Scope: {}", access_token.scope);
73 println!("Session Key: {}", access_token.session_key);
74 println!("Session Secret: {}", access_token.session_secret);
75 println!("Acquired At: {}", access_token.acquired_at);
76 println!();
77
78 println!("=== Auth Flow Completed Successfully ===");
79
80 Ok(())
81}