librespot/oauth/examples/oauth.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

32 lines
1 KiB
Rust

use std::env;
use librespot_oauth::get_access_token;
const SPOTIFY_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd";
const SPOTIFY_REDIRECT_URI: &str = "http://127.0.0.1:8898/login";
fn main() {
let mut builder = env_logger::Builder::new();
builder.parse_filters("librespot=trace");
builder.init();
let args: Vec<_> = env::args().collect();
let (client_id, redirect_uri, scopes) = if args.len() == 4 {
// You can use your own client ID, along with it's associated redirect URI.
(
args[1].as_str(),
args[2].as_str(),
args[3].split(',').collect::<Vec<&str>>(),
)
} else if args.len() == 1 {
(SPOTIFY_CLIENT_ID, SPOTIFY_REDIRECT_URI, vec!["streaming"])
} else {
eprintln!("Usage: {} [CLIENT_ID REDIRECT_URI SCOPES]", args[0]);
return;
};
match get_access_token(client_id, redirect_uri, scopes) {
Ok(token) => println!("Success: {token:#?}"),
Err(e) => println!("Failed: {e}"),
};
}