summaryrefslogtreecommitdiff
path: root/examples/access_token/main.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-09-05 17:57:28 +0200
committerHampusM <hampus@hampusmat.com>2022-09-05 17:57:28 +0200
commitd7929e7e9fee879a28871c2195620869db291441 (patch)
tree4b2c9a6b3bf64144a994d36d0a522404548116f3 /examples/access_token/main.rs
parentafade48668042eeb0740c7c5a1cc3806baaedc94 (diff)
feat: add authentication
Diffstat (limited to 'examples/access_token/main.rs')
-rw-r--r--examples/access_token/main.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/access_token/main.rs b/examples/access_token/main.rs
new file mode 100644
index 0000000..d9b4d79
--- /dev/null
+++ b/examples/access_token/main.rs
@@ -0,0 +1,52 @@
+use std::error::Error;
+
+use anyhow::Context;
+use config::Config;
+use deez::auth::{request_access_token, AuthPromptHandler};
+use serde::Deserialize;
+
+#[derive(Deserialize)]
+struct Settings
+{
+ app_id: u32,
+ secret_key: String,
+ address: String,
+ port: u16,
+ uri_scheme: String,
+}
+
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn Error + Send + Sync>>
+{
+ let settings = Config::builder()
+ .add_source(config::File::with_name("examples/access_token/Settings"))
+ .build()
+ .with_context(|| "Failed to read settings")?
+ .try_deserialize::<Settings>()
+ .with_context(|| "Invalid settings")?;
+
+ let auth_code_prompt_info = AuthPromptHandler::run(
+ settings.app_id,
+ settings.address,
+ settings.port,
+ settings.uri_scheme,
+ )
+ .await?;
+
+ println!("{}", auth_code_prompt_info.auth_prompt_url);
+
+ let auth_code = auth_code_prompt_info.handler.await??;
+
+ println!("Retrieved authentication code '{}'", auth_code);
+
+ let access_token =
+ request_access_token(settings.app_id, settings.secret_key, auth_code).await?;
+
+ println!(
+ "Retrieved access token '{}' which expires in {} seconds",
+ access_token.access_token,
+ access_token.expires.as_secs()
+ );
+
+ Ok(())
+}