-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathwith_token_callback_fn.rs
97 lines (84 loc) · 3.56 KB
/
with_token_callback_fn.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! If developer specify a callback function for token, this callback function
//! will be invoked whenever client succeeds to request or refetch a token.
//! Therefore, developer could write token into file or save token into database
//! after fetch the token with their own callback function.
use std::sync::Arc;
use rspotify::{
clients::OAuthClient, scopes, AuthCodePkceSpotify, AuthCodeSpotify, CallbackError,
ClientCredsSpotify, Config, Credentials, OAuth, TokenCallback,
};
async fn _with_pkce(creds: Credentials, oauth: OAuth) {
let operate_token_fn = |token| {
println!(">>> From token callback function with AuthCodePkceSpotify");
println!(">>> Let's manipulate it. Oooh, we could only read it");
println!(">>> token: {:?}", token);
Err(CallbackError::CustomizedError(
"oooh, there is an error".to_string(),
))
};
let token_callback = TokenCallback(Box::new(operate_token_fn));
// Enabling automatic token refreshing in the config
let config = Config {
token_callback_fn: Arc::new(Some(token_callback)),
..Default::default()
};
let mut spotify = AuthCodePkceSpotify::with_config(creds, oauth, config);
let url = spotify.get_authorize_url(None).unwrap();
// This function requires the `cli` feature enabled.
spotify
.prompt_for_token(&url)
.await
.expect("couldn't authenticate successfully");
}
async fn with_auth(creds: Credentials, oauth: OAuth) {
let operate_token_fn = |token| {
println!(">>> From token callback function with AuthCodeSpotify");
println!(">>> Let's manipulate it. Oooh, we could only read it");
println!(">>> token: {:?}", token);
Ok(())
};
let token_callback = TokenCallback(Box::new(operate_token_fn));
// Enabling automatic token refreshing in the config
let config = Config {
token_callback_fn: Arc::new(Some(token_callback)),
..Default::default()
};
println!(">>> Fetch token with AuthCodeSpotify");
let spotify = AuthCodeSpotify::with_config(creds, oauth, config);
let url = spotify.get_authorize_url(false).unwrap();
// This function requires the `cli` feature enabled.
spotify
.prompt_for_token(&url)
.await
.expect("couldn't authenticate successfully");
}
async fn with_client_credentials(creds: Credentials) {
let operate_token_fn = |token| {
println!(">>> From token callback function with ClientCredsSpotify");
println!(">>> Let's manipulate it. Oooh, we could only read it");
println!(">>> token: {:?}", token);
Ok(())
};
let token_callback = TokenCallback(Box::new(operate_token_fn));
// Enabling automatic token refreshing in the config
let config = Config {
token_callback_fn: Arc::new(Some(token_callback)),
..Default::default()
};
// Same with client-credential based spotify client
println!(">>> Fetch token with ClientCredsSpotify");
let spotify = ClientCredsSpotify::with_config(creds, config);
spotify.request_token().await.unwrap();
}
#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();
// May require the `env-file` feature enabled if the environment variables
// aren't configured manually.
let creds = Credentials::from_env().unwrap();
let oauth = OAuth::from_env(scopes!("user-follow-read user-follow-modify")).unwrap();
with_auth(creds.clone(), oauth.clone()).await;
// with_pkce(creds.clone(), oauth).await;
with_client_credentials(creds).await;
}