msgraph_rs/resources/auth/
token.rs1use reqwest::blocking::Client;
2use serde::{Deserialize, Serialize};
3use std::error::Error;
4
5const TOKEN_URL: &str = "https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token";
6
7#[derive(Serialize)]
8struct TokenRequest {
9 client_id: String,
10 scope: String,
11 client_secret: String,
12 grant_type: String,
13}
14
15#[derive(Deserialize)]
16pub struct TokenResponse {
17 pub access_token: String,
18}
19
20pub fn get_access_token(
21 client_id: &str,
22 client_secret: &str,
23 tenant_id: &str,
24) -> Result<String, Box<dyn Error>> {
25 let url = TOKEN_URL.replace("{tenant_id}", tenant_id);
26
27 let client = Client::new();
28 let body = TokenRequest {
29 client_id: client_id.to_string(),
30 scope: "https://graph.microsoft.com/.default".to_string(),
31 client_secret: client_secret.to_string(),
32 grant_type: "client_credentials".to_string(),
33 };
34
35 let res: TokenResponse = client.post(&url).form(&body).send()?.json()?;
36
37 Ok(res.access_token)
38}