-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_oauth.rs
46 lines (36 loc) · 1.35 KB
/
auth_oauth.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
mod oauth;
use rusty_box::{BoxAPIError, BoxClient, Config, OAuth};
use crate::oauth::{authorize_app, storage};
use std::env;
#[tokio::main]
async fn main() -> Result<(), BoxAPIError> {
dotenv::from_filename(".oauth.env").expect("Failed to read .env file");
let client_id = env::var("CLIENT_ID").expect("CLIENT_ID not set");
let client_secret = env::var("CLIENT_SECRET").expect("CLIENT_SECRET not set");
let redirect_uri = env::var("REDIRECT_URI").expect("REDIRECT_URI not set");
let config = Config::new();
let oauth = OAuth::new(
config,
client_id,
client_secret,
Some(storage::save_access_token),
);
// Load the OAuth token from the cache file
let oauth_json = oauth::storage::load_access_token();
let oauth = match oauth_json {
Ok(oauth_json) => {
println!("Cached token found, refreshing");
let oauth: OAuth =
serde_json::from_str(&oauth_json).expect("Failed to parse cached token");
oauth
}
Err(_) => {
println!("No cached token found, authorizing app");
authorize_app::authorize_app(oauth, Some(redirect_uri)).await?
}
};
let mut client = BoxClient::new(Box::new(oauth));
let me = rusty_box::users_api::me(&mut client, None).await;
println!("Me:\n{me:#?}\n");
Ok(())
}