Skip to content

Commit

Permalink
Example for device code flow added (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
fratschi authored Sep 5, 2023
1 parent 20967dd commit 43d4e99
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions sdk/identity/examples/device_code_flow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use azure_core::new_http_client;
use azure_identity::device_code_flow::start;
use futures::{future::ready, StreamExt};
use oauth2::ClientId;
use std::{env, error::Error};

const SCOPES: &[&str; 2] = &[".default", "offline_access"];

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
let client_id =
ClientId::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));

let client = new_http_client();

match start(client.clone(), tenant_id, client_id.as_str(), SCOPES).await {
Ok(response) => {
println!("{:?}", response.message());

response
.stream()
.for_each(|result| {
match result {
Ok(value) => {
println!("access token: {:?}", value.access_token().secret());
match value.refresh_token() {
None => {}
Some(tk) => println!("refresh token: {:?}", tk.secret()),
}
}
Err(_) => println!("waiting..."),
}
ready(())
})
.await
}
Err(err) => println!("error: {:?}", err),
}

Ok(())
}

0 comments on commit 43d4e99

Please sign in to comment.