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));
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();
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));
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();
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));
let config = Config {
token_callback_fn: Arc::new(Some(token_callback)),
..Default::default()
};
println!(">>> Fetch token with ClientCredsSpotify");
let spotify = ClientCredsSpotify::with_config(creds, config);
spotify.request_token().await.unwrap();
}
#[tokio::main]
async fn main() {
env_logger::init();
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_client_credentials(creds).await;
}