librespot/examples/playlist_tracks.rs
Nick Steel 4f9151c642
Credentials with access token (oauth) (#1309)
* core: Create credentials from access token via OAuth2

* core: Credentials.username is optional: not required for token auth.

* core: store auth data within session. We might need this later if need to re-auth and original creds are no longer valid/available.

* bin: New --token arg for using Spotify access token. Specify 0 to manually enter the auth code (headless).

* bin: Added --enable-oauth / -j option. Using --password / -p option will error and exit.

* core: reconnect session if using token authentication

Token authenticated sessions cannot use keymaster. So reconnect using the reusable credentials we just obtained. Can perhaps remove this
workaround once keymaster is replaced with login5.

* examples: replace password login with token login
2024-09-13 07:35:55 +02:00

43 lines
1.2 KiB
Rust

use std::{env, process::exit};
use librespot::{
core::{
authentication::Credentials, config::SessionConfig, session::Session, spotify_id::SpotifyId,
},
metadata::{Metadata, Playlist, Track},
};
#[tokio::main]
async fn main() {
env_logger::init();
let session_config = SessionConfig::default();
let args: Vec<_> = env::args().collect();
if args.len() != 3 {
eprintln!("Usage: {} ACCESS_TOKEN PLAYLIST", args[0]);
return;
}
let credentials = Credentials::with_access_token(&args[1]);
let plist_uri = SpotifyId::from_uri(&args[2]).unwrap_or_else(|_| {
eprintln!(
"PLAYLIST should be a playlist URI such as: \
\"spotify:playlist:37i9dQZF1DXec50AjHrNTq\""
);
exit(1);
});
let session = Session::new(session_config, None);
if let Err(e) = session.connect(credentials, false).await {
println!("Error connecting: {}", e);
exit(1);
}
let plist = Playlist::get(&session, &plist_uri).await.unwrap();
println!("{:?}", plist);
for track_id in plist.tracks() {
let plist_track = Track::get(&session, track_id).await.unwrap();
println!("track: {} ", plist_track.name);
}
}