Skip to content

Commit

Permalink
Updated token example
Browse files Browse the repository at this point in the history
  • Loading branch information
kingosticks committed Aug 16, 2024
1 parent fe4d36b commit bf8b003
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions examples/get_token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;

use librespot::core::{authentication::Credentials, config::SessionConfig, session::Session};
use librespot::protocol::authentication::AuthenticationType;

const SCOPES: &str =
"streaming,user-read-playback-state,user-modify-playback-state,user-read-currently-playing";
Expand All @@ -10,36 +11,43 @@ async fn main() {
let mut session_config = SessionConfig::default();

let args: Vec<_> = env::args().collect();
if args.len() == 4 {
session_config.client_id = args[3].clone()
} else if args.len() != 3 {
eprintln!("Usage: {} USERNAME PASSWORD [CLIENT_ID]", args[0]);
if args.len() == 3 {
session_config.client_id = args[2].clone()
} else if args.len() != 2 {
eprintln!("Usage: {} ACCESS_TOKEN [CLIENT_ID]", args[0]);
return;
}
let username = &args[1];
let password = &args[2];
let access_token = &args[1];

// Now create a new session with that token.
let session = Session::new(session_config.clone(), None);
let credentials = Credentials::with_password(username, password);
println!("Connecting with password..");
let token = match session.connect(credentials, false).await {
Ok(()) => {
println!("Session username: {:#?}", session.username());
session.token_provider().get_token(SCOPES).await.unwrap()
let credentials = Credentials::with_access_token(access_token);
println!("Connecting with token..");
match session.connect(credentials, false).await {
Ok(()) => println!("Session username: {:#?}", session.username()),
Err(e) => {
println!("Error connecting: {e}");
return;
}
};

// This will fail. You can't use keymaster from an access token authed session.
// let token = session2.token_provider().get_token(SCOPES).await.unwrap();

// Instead, derive stored credentials and auth a new session using those.
let stored_credentials = Credentials {
username: Some(session.username()),
auth_type: AuthenticationType::AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS,
auth_data: session.auth_data(),
};
let session2 = Session::new(session_config, None);
match session2.connect(stored_credentials, false).await {
Ok(()) => println!("Session username: {:#?}", session2.username()),
Err(e) => {
println!("Error connecting: {}", e);
return;
}
};
println!("Token: {:#?}", token);

// Now create a new session with that token.
let session = Session::new(session_config, None);
let credentials = Credentials::with_access_token(token.access_token);
println!("Connecting with token..");
match session.connect(credentials, false).await {
Ok(()) => println!("Session username: {:#?}", session.username()),
Err(e) => println!("Error connecting: {}", e),
}
let token = session2.token_provider().get_token(SCOPES).await.unwrap();
println!("Got me a token: {token:#?}");
}

0 comments on commit bf8b003

Please sign in to comment.