librespot/core/src/keymaster.rs

27 lines
719 B
Rust
Raw Normal View History

use serde::Deserialize;
2021-01-21 20:49:39 +00:00
use crate::{mercury::MercuryError, session::Session};
2017-05-10 15:26:48 +00:00
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Token {
pub access_token: String,
pub expires_in: u32,
pub token_type: String,
pub scope: Vec<String>,
}
2021-01-21 20:49:39 +00:00
pub async fn get_token(
2018-02-11 11:37:08 +00:00
session: &Session,
client_id: &str,
scopes: &str,
2021-01-21 20:49:39 +00:00
) -> Result<Token, MercuryError> {
2018-02-11 11:37:08 +00:00
let url = format!(
"hm://keymaster/token/authenticated?client_id={}&scope={}",
client_id, scopes
);
2021-01-21 20:49:39 +00:00
let response = session.mercury().get(url).await?;
let data = response.payload.first().expect("Empty payload");
serde_json::from_slice(data.as_ref()).map_err(|_| MercuryError)
2017-05-10 15:26:48 +00:00
}