-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example for device code flow added (#1328)
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 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
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(()) | ||
} |