forked from librespot-org/librespot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OAuth process made by a struct, allowing customization options. (libr…
…espot-org#1462) * get refresh token. Optional auth url browser opening * changelog * access token accepts custom message * docs updated * CustomParams renamed * OAuthToken can be cloned * builder pattern on token management * changelog * docs and format issues * split methods and finish documentation * new example and minor adjustments * typo * remove unnecessary dependency * requested changes * Update oauth/src/lib.rs Co-authored-by: Felix Prillwitz <photovoltex@mailbox.org> * Update oauth/src/lib.rs Co-authored-by: Felix Prillwitz <photovoltex@mailbox.org> * Update CHANGELOG.md Co-authored-by: Felix Prillwitz <photovoltex@mailbox.org> * Update oauth/src/lib.rs Co-authored-by: Felix Prillwitz <photovoltex@mailbox.org> * Update oauth/src/lib.rs Co-authored-by: Felix Prillwitz <photovoltex@mailbox.org> * Update oauth/src/lib.rs Co-authored-by: Nick Steel <nick@nsteel.co.uk> * Update oauth/src/lib.rs Co-authored-by: Nick Steel <nick@nsteel.co.uk> * remove veil. Oauth flow fix * debug trait instead of veil * Update main.rs Co-authored-by: Nick Steel <nick@nsteel.co.uk> --------- Co-authored-by: Felix Prillwitz <photovoltex@mailbox.org> Co-authored-by: Nick Steel <nick@nsteel.co.uk>
- Loading branch information
1 parent
581c8d6
commit f497806
Showing
8 changed files
with
474 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::env; | ||
|
||
use librespot_oauth::OAuthClientBuilder; | ||
|
||
const SPOTIFY_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd"; | ||
const SPOTIFY_REDIRECT_URI: &str = "http://127.0.0.1:8898/login"; | ||
|
||
const RESPONSE: &str = r#" | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<h1>Return to your app!</h1> | ||
</body> | ||
</html> | ||
"#; | ||
|
||
#[tokio::main] | ||
async 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; | ||
}; | ||
|
||
let client = match OAuthClientBuilder::new(client_id, redirect_uri, scopes) | ||
.open_in_browser() | ||
.with_custom_message(RESPONSE) | ||
.build() | ||
{ | ||
Ok(client) => client, | ||
Err(err) => { | ||
eprintln!("Unable to build an OAuth client: {}", err); | ||
return; | ||
} | ||
}; | ||
|
||
let refresh_token = match client.get_access_token_async().await { | ||
Ok(token) => { | ||
println!("OAuth Token: {token:#?}"); | ||
token.refresh_token | ||
} | ||
Err(err) => { | ||
println!("Unable to get OAuth Token: {err}"); | ||
return; | ||
} | ||
}; | ||
|
||
match client.refresh_token_async(&refresh_token).await { | ||
Ok(token) => println!("New refreshed OAuth Token: {token:#?}"), | ||
Err(err) => println!("Unable to get refreshed OAuth Token: {err}"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::env; | ||
|
||
use librespot_oauth::OAuthClientBuilder; | ||
|
||
const SPOTIFY_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd"; | ||
const SPOTIFY_REDIRECT_URI: &str = "http://127.0.0.1:8898/login"; | ||
|
||
const RESPONSE: &str = r#" | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<h1>Return to your app!</h1> | ||
</body> | ||
</html> | ||
"#; | ||
|
||
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; | ||
}; | ||
|
||
let client = match OAuthClientBuilder::new(client_id, redirect_uri, scopes) | ||
.open_in_browser() | ||
.with_custom_message(RESPONSE) | ||
.build() | ||
{ | ||
Ok(client) => client, | ||
Err(err) => { | ||
eprintln!("Unable to build an OAuth client: {}", err); | ||
return; | ||
} | ||
}; | ||
|
||
let refresh_token = match client.get_access_token() { | ||
Ok(token) => { | ||
println!("OAuth Token: {token:#?}"); | ||
token.refresh_token | ||
} | ||
Err(err) => { | ||
println!("Unable to get OAuth Token: {err}"); | ||
return; | ||
} | ||
}; | ||
|
||
match client.refresh_token(&refresh_token) { | ||
Ok(token) => println!("New refreshed OAuth Token: {token:#?}"), | ||
Err(err) => println!("Unable to get refreshed OAuth Token: {err}"), | ||
} | ||
} |
Oops, something went wrong.