-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an example to fetch a Spotify API token #512
Conversation
.unwrap(); | ||
|
||
println!( | ||
"Token: {:#?}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that print something like this with a duplicate Token
prefix?
Token: Token {
access_token: "BQCfSS6x...",
Could this easily be changed to print raw, parsable JSON instead? As the keys aren't quoted, parsers would fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was is just meant to be an example on how to use the API, JSON is doable, but then I would rather call the mercury (hm) endpoint directly that spits out JSON.
Or you could always manually build up the JSON string with format!
.
diff --git a/examples/get_token.rs b/examples/get_token.rs
index d722e99..5300946 100644
--- a/examples/get_token.rs
+++ b/examples/get_token.rs
@@ -28,10 +28,11 @@ fn main() {
let session = core
.run(Session::connect(session_config, credentials, None, handle))
.unwrap();
-
+ let token = core
+ .run(keymaster::get_token(&session, &client_id, SCOPES))
+ .unwrap();
println!(
- "Token: {:#?}",
- core.run(keymaster::get_token(&session, &client_id, SCOPES))
- .unwrap()
+ "{{\"accessToken\":\"{}\",\"expiresIn\":{}}}",
+ token.access_token, token.expires_in
);
}
And this would be the "correct" way to do it
diff --git a/Cargo.lock b/Cargo.lock
index 533b47d..ec25bbd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -951,6 +951,7 @@ dependencies = [
"protobuf 2.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rpassword 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
"sha-1 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index e2dfda2..4fa1ec9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -58,6 +58,9 @@ url = "1.7"
sha-1 = "0.8"
hex = "0.3"
+[dev-dependencies]
+serde_json = "1"
+
[features]
alsa-backend = ["librespot-playback/alsa-backend"]
portaudio-backend = ["librespot-playback/portaudio-backend"]
diff --git a/core/src/keymaster.rs b/core/src/keymaster.rs
index f2d7b77..4ac55b9 100644
--- a/core/src/keymaster.rs
+++ b/core/src/keymaster.rs
@@ -4,7 +4,7 @@ use serde_json;
use crate::mercury::MercuryError;
use crate::session::Session;
-#[derive(Deserialize, Debug, Clone)]
+#[derive(Deserialize, Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Token {
pub access_token: String,
diff --git a/examples/get_token.rs b/examples/get_token.rs
index d722e99..0a9761b 100644
--- a/examples/get_token.rs
+++ b/examples/get_token.rs
@@ -5,6 +5,7 @@ use librespot::core::authentication::Credentials;
use librespot::core::config::SessionConfig;
use librespot::core::keymaster;
use librespot::core::session::Session;
+use serde_json;
const SCOPES: &str =
"streaming,user-read-playback-state,user-modify-playback-state,user-read-currently-playing";
@@ -28,10 +29,8 @@ fn main() {
let session = core
.run(Session::connect(session_config, credentials, None, handle))
.unwrap();
-
- println!(
- "Token: {:#?}",
- core.run(keymaster::get_token(&session, &client_id, SCOPES))
- .unwrap()
- );
+ let token = core
+ .run(keymaster::get_token(&session, &client_id, SCOPES))
+ .unwrap();
+ println!("{:#?}", serde_json::to_string(&token).unwrap());
}
Which will dump
"{\"accessToken\":\"BQC1GSN7OMde-PXdam-MfJuO0-B6g_tGebPF snipity snip snip\",\"expiresIn\":3600,\"tokenType\":\"Bearer\",\"scope\":[\"streaming\",\"user-read-playback-state\",\"user-modify-playback-state\",\"user-read-currently-playing\"]}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, understood, thanks. But my comment about the double "Token" prefix still is true, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I follow, the original example prints the Token
struct using the Debug trait. This isn't necessary valid JSON..
Are you suggesting to remove the Token:
prefix in the string printed to stdout to make it easier to parse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe what I'm seeing is that if you dump the Token
struct, that word Token
would already be part of the dump. No need to print it in the println
statement (but then I don't have the tools here to double check). Nothing to do with JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was just following the variable: {value}
nomenclature. Especially given this is an example, it should make things explicit?
Something that gets asked quite often - so here goes!