use rspotify::{
model::ArtistId, prelude::*, scopes, AuthCodePkceSpotify, AuthCodeSpotify, Credentials, OAuth,
};
async fn do_things(spotify: &AuthCodeSpotify) {
let artists = [
ArtistId::from_id("3RGLhK1IP9jnYFH4BRFJBS").unwrap(), ArtistId::from_id("0yNLKJebCb8Aueb54LYya3").unwrap(), ArtistId::from_id("2jzc5TC5TVFLXQlBNiIUzE").unwrap(), ];
let num_artists = artists.len();
spotify
.user_follow_artists(artists.iter().map(|a| a.as_ref()))
.await
.expect("couldn't follow artists");
println!("Followed {num_artists} artists successfully.");
let followed = spotify
.current_user_followed_artists(None, None)
.await
.expect("couldn't get user followed artists");
println!(
"User currently follows at least {} artists.",
followed.items.len()
);
spotify
.user_unfollow_artists(artists)
.await
.expect("couldn't unfollow artists");
println!("Unfollowed {num_artists} artists successfully.");
}
async fn pkce_do_things(spotify: &AuthCodePkceSpotify) {
let artists = [
ArtistId::from_id("3RGLhK1IP9jnYFH4BRFJBS").unwrap(), ArtistId::from_id("0yNLKJebCb8Aueb54LYya3").unwrap(), ArtistId::from_id("2jzc5TC5TVFLXQlBNiIUzE").unwrap(), ];
let num_artists = artists.len();
spotify
.user_follow_artists(artists.iter().map(|a| a.as_ref()))
.await
.expect("AuthCodePkceSpotify couldn't follow artists");
println!("AuthCodePkceSpotify Followed {num_artists} artists successfully.");
let followed = spotify
.current_user_followed_artists(None, None)
.await
.expect("couldn't get user followed artists");
println!(
"AuthCodePkceSpotify User currently follows at least {} artists.",
followed.items.len()
);
spotify
.user_unfollow_artists(artists)
.await
.expect("couldn't unfollow artists");
println!("AuthCodePkceSpotify Unfollowed {num_artists} artists successfully.");
}
async fn refresh_pkce_code(creds: Credentials, oauth: OAuth) {
let mut spotify = AuthCodePkceSpotify::new(creds.clone(), oauth.clone());
let url = spotify.get_authorize_url(None).unwrap();
spotify.prompt_for_token(&url).await.unwrap();
pkce_do_things(&spotify).await;
println!(">>> Pkce Session two, running some requests:");
spotify
.refresh_token()
.await
.expect("couldn't refresh user token");
pkce_do_things(&spotify).await;
println!(">>> Pkce Session three, running some requests:");
spotify
.refresh_token()
.await
.expect("couldn't refresh user token");
pkce_do_things(&spotify).await;
}
async fn refresh_auth_code(creds: Credentials, oauth: OAuth) {
let spotify = AuthCodeSpotify::new(creds.clone(), oauth.clone());
println!(">>> Session one, obtaining refresh token and running some requests:");
let url = spotify.get_authorize_url(false).unwrap();
spotify
.prompt_for_token(&url)
.await
.expect("couldn't authenticate successfully");
do_things(&spotify).await;
println!(">>> Session two, running some requests:");
spotify
.refresh_token()
.await
.expect("couldn't refresh user token");
do_things(&spotify).await;
println!(">>> Session three, running some requests:");
spotify
.refresh_token()
.await
.expect("couldn't refresh user token");
do_things(&spotify).await;
}
#[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();
refresh_auth_code(creds.clone(), oauth.clone()).await;
refresh_pkce_code(creds, oauth).await;
}