Zig
Zig
Microsoft Graph -- List Users (Automatic OAuth2 Client Credentials)
See more Microsoft Graph Examples
Simplify OAuth2 client credentials by using Chilkat to automatically obtain the necessary access token. This example demonstrates the technique with the Microsoft Graph API's "List Users" request.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Instead of directly sending a POST request to obtain an OAuth2 access token,
// you can provide Chilkat with the necessary information to automatically fetch
// the token using the Client Credentials flow. During the first HTTP request,
// Chilkat will obtain the token if it doesn't have one yet, and then use it for
// all subsequent requests. When the token is expired or about to expire, Chilkat
// will automatically obtain a new one, eliminating the need for your application
// to manage token expiration and renewal manually. This approach also reduces the
// number of HTTP requests by utilizing valid tokens as long as possible.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("client_secret", "CLIENT_SECRET") catch {};
json.updateString("client_id", "CLIENT_ID") catch {};
json.updateString("token_endpoint", "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token") catch {};
json.updateString("scope", "https://graph.microsoft.com/.default") catch {};
const http = try chilkat.Http.init();
defer http.deinit();
// Provide the information to let Chilkat automatically obtain the access token.
http.setAuthToken(try json.emit(alloc));
const sb_response = try chilkat.StringBuilder.init();
defer sb_response.deinit();
http.quickGetSb("https://graph.microsoft.com/v1.0/users", sb_response) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
json.loadSb(sb_response) catch {};
json.setEmitCompact(false);
std.debug.print("Status code = {d}\n", .{http.getLastStatus()});
if (http.getLastStatus() != 200) {
std.debug.print("{s}\n", .{try json.emit(alloc)});
std.debug.print("Failed.\n", .{});
}
std.debug.print("{s}\n", .{try json.emit(alloc)});
// 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
var odata_id: [:0]const u8 = "";
var display_name: [:0]const u8 = "";
var given_name: [:0]const u8 = "";
var job_title: [:0]const u8 = "";
var mail: [:0]const u8 = "";
var mobile_phone: [:0]const u8 = "";
var office_location: [:0]const u8 = "";
var preferred_language: [:0]const u8 = "";
var surname: [:0]const u8 = "";
var user_principal_name: [:0]const u8 = "";
var id: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
_ = try json.stringOf(alloc, "\"@odata.context\"");
var i: i32 = 0;
const count_i = json.sizeOfArray("value");
while (i < count_i) {
json.setI(i);
odata_id = try json.stringOf(alloc, "value[i].\"@odata.id\"");
display_name = try json.stringOf(alloc, "value[i].displayName");
given_name = try json.stringOf(alloc, "value[i].givenName");
job_title = try json.stringOf(alloc, "value[i].jobTitle");
mail = try json.stringOf(alloc, "value[i].mail");
mobile_phone = try json.stringOf(alloc, "value[i].mobilePhone");
office_location = try json.stringOf(alloc, "value[i].officeLocation");
preferred_language = try json.stringOf(alloc, "value[i].preferredLanguage");
surname = try json.stringOf(alloc, "value[i].surname");
user_principal_name = try json.stringOf(alloc, "value[i].userPrincipalName");
id = try json.stringOf(alloc, "value[i].id");
j = 0;
count_j = json.sizeOfArray("value[i].businessPhones");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
i = i + 1;
}
}