Rust
Rust
Microsoft Graph -- List Users
See more Microsoft Graph Examples
Retrieve a list of Microsoft Graph user objects.Chilkat Rust Downloads
// ------------------------------------------------------------------------------------------------------
// See an easier way to do OAuth2 client credentials:
// Example using Automatic OAuth2 Client Credentials
// ------------------------------------------------------------------------------------------------------
// Get an access token with the required scope using client credentials...
// See How to Create Microsoft Graph App (in Azure Portal) for Client Credentials Authentication
let http = chilkat::Http::new();
let req = chilkat::HttpRequest::new();
req.add_param("client_secret", "CLIENT_SECRET");
req.add_param("client_id", "CLIENT_ID");
req.add_param("scope", "https://graph.microsoft.com/.default");
req.add_param("grant_type", "client_credentials");
// Use your own tenant ID, for example 4d8fdd66-66d1-43b0-ae5c-e31b4b7de5cd
let url = "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token".to_string();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
let resp = chilkat::HttpResponse::new();
if http.http_req(&url, &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let status_code = resp.status_code();
println!("Response status code = {}", status_code);
let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str());
// -----------------------------------------------------
// Use the access token obtained from above.
// Note: We don't need to re-fetch a new access token every time. An access token is valid
// for some amount of time, typically an hour (3600 seconds)
// Use your previously obtained access token here:
http.set_auth_token(&json.string_of("access_token").unwrap_or_default());
println!("access token: {}", http.auth_token());
let sb_response = chilkat::StringBuilder::new();
if http.quick_get_sb("https://graph.microsoft.com/v1.0/users", &sb_response).is_err() {
println!("{}", http.last_error_text());
return;
}
let _ = json.load_sb(&sb_response);
json.set_emit_compact(false);
println!("Status code = {}", http.last_status());
if http.last_status() != 200 {
println!("{}", json.emit().unwrap_or_default());
println!("Failed.");
}
println!("{}", json.emit().unwrap_or_default());
// Sample output
// {
// "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
// "value": [
// {
// "@odata.id": "https://graph.microsoft.com/v2/6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/directoryObjects/fca490d8-5918-4201-8079-c5dcbeafcdc9/Microsoft.DirectoryServices.User",
// "businessPhones": [
// ],
// "displayName": "Joe Sample",
// "givenName": "Joe",
// "jobTitle": null,
// "mail": null,
// "mobilePhone": null,
// "officeLocation": null,
// "preferredLanguage": null,
// "surname": "Sample",
// "userPrincipalName": "admin_chilkatsoft.com#EXT#@adminchilkatsoft.onmicrosoft.com",
// "id": "fca490d8-5918-4201-8079-c5dcbeafcdc9"
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let odata_context = json.string_of("\"@odata.context\"").unwrap_or_default();
let mut i = 0;
let count_i = json.size_of_array("value");
while i < count_i {
json.set_i(i);
let _ = json.string_of("value[i].\"@odata.id\"").unwrap_or_default();
let _ = json.string_of("value[i].displayName").unwrap_or_default();
let _ = json.string_of("value[i].givenName").unwrap_or_default();
let _ = json.string_of("value[i].jobTitle").unwrap_or_default();
let _ = json.string_of("value[i].mail").unwrap_or_default();
let _ = json.string_of("value[i].mobilePhone").unwrap_or_default();
let _ = json.string_of("value[i].officeLocation").unwrap_or_default();
let _ = json.string_of("value[i].preferredLanguage").unwrap_or_default();
let _ = json.string_of("value[i].surname").unwrap_or_default();
let _ = json.string_of("value[i].userPrincipalName").unwrap_or_default();
let _ = json.string_of("value[i].id").unwrap_or_default();
j = 0;
count_j = json.size_of_array("value[i].businessPhones");
while j < count_j {
json.set_j(j);
j = j + 1;
}
i = i + 1;
}