Sample code for 30+ languages & platforms
Zig

Microsoft Graph -- List Users

See more Microsoft Graph Examples

Retrieve a list of Microsoft Graph user objects.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // ------------------------------------------------------------------------------------------------------
    // 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
    const http = try chilkat.Http.init();
    defer http.deinit();
    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.addParam("client_secret", "CLIENT_SECRET");
    req.addParam("client_id", "CLIENT_ID");
    req.addParam("scope", "https://graph.microsoft.com/.default");
    req.addParam("grant_type", "client_credentials");

    // Use your own tenant ID, for example 4d8fdd66-66d1-43b0-ae5c-e31b4b7de5cd
    const url = "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token";

    req.setHttpVerb("POST");
    req.setContentType("application/x-www-form-urlencoded");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq(url, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const status_code = resp.getStatusCode();
    std.debug.print("Response status code = {d}\n", .{status_code});

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(try resp.getBodyStr(alloc)) catch {};

    // -----------------------------------------------------
    // 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.setAuthToken(try json.stringOf(alloc, "access_token"));

    std.debug.print("access token: {s}\n", .{try http.getAuthToken(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;
    }
}