Skip to main content

refresh_token/
refresh_token.rs

1use chrono::offset::Utc;
2
3#[tokio::main]
4async fn main() {
5
6    let client = ksef_client::KsefClient::new("https://api.ksef.mf.gov.pl".to_string(), 2000).unwrap();
7
8    let company_info = ksef_client::CompanyInfo {
9        ksef_token: "<ksef_token>".to_string(),
10        nip: "<nip>".to_string(),
11    };
12
13    let access_tokens = match client.get_access_tokens(&company_info).await {
14        Ok(access_tokens) => access_tokens,
15        Err(e) => {
16            eprintln!("Error getting access_tokens: {}", e);
17            return;
18        }
19    };
20
21    // if access token has expired
22    if access_tokens.access_token.valid_until < Utc::now() {
23        // refresh token ->
24        let access_token_info = match client
25            .refresh_access_token(&access_tokens.refresh_token.token)
26            .await
27        {
28            Ok(access_token_info) => access_token_info,
29            Err(e) => {
30                eprintln!("Error getting refresh token: {}", e);
31                return;
32            }
33        };
34
35        let access_token = access_token_info.token;
36
37        println!("access_token {}", access_token);
38    }
39}