-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Microsoft device code example with tenant ID (#222)
To make it more understandable for Microsoft Azure endpoint users, an endpoint with tenant is added and edited the existing example as common user.
- Loading branch information
1 parent
49d8798
commit 5f4c288
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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,51 @@ | ||
use oauth2::basic::BasicClient; | ||
use oauth2::devicecode::StandardDeviceAuthorizationResponse; | ||
use oauth2::reqwest::async_http_client; | ||
use oauth2::{AuthUrl, ClientId, DeviceAuthorizationUrl, Scope, TokenUrl}; | ||
use std::error::Error; | ||
|
||
// Reference: https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code | ||
// Please use your tenant id when using this example | ||
const TENANT_ID: &str = "{tenant}"; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
let device_auth_url = DeviceAuthorizationUrl::new(format!( | ||
"https://login.microsoftonline.com/{}/oauth2/v2.0/devicecode", | ||
TENANT_ID | ||
))?; | ||
let client = BasicClient::new( | ||
ClientId::new("client_id".to_string()), | ||
None, | ||
AuthUrl::new(format!( | ||
"https://login.microsoftonline.com/{}/oauth2/v2.0/authorize", | ||
TENANT_ID | ||
))?, | ||
Some(TokenUrl::new(format!( | ||
"https://login.microsoftonline.com/{}/oauth2/v2.0/token", | ||
TENANT_ID | ||
))?), | ||
) | ||
.set_device_authorization_url(device_auth_url); | ||
|
||
let details: StandardDeviceAuthorizationResponse = client | ||
.exchange_device_code()? | ||
.add_scope(Scope::new("read".to_string())) | ||
.request_async(async_http_client) | ||
.await?; | ||
|
||
eprintln!( | ||
"Open this URL in your browser:\n{}\nand enter the code: {}", | ||
details.verification_uri().to_string(), | ||
details.user_code().secret().to_string() | ||
); | ||
|
||
let token_result = client | ||
.exchange_device_access_token(&details) | ||
.request_async(async_http_client, tokio::time::sleep, None) | ||
.await; | ||
|
||
eprintln!("Token:{:?}", token_result); | ||
|
||
Ok(()) | ||
} |