Sample code for 30+ languages & platforms
Zig

Outlook Calendar List Calendars

See more Outlook Calendar Examples

Get all the user's calendars.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Use your previously obtained access token here: Get Outlook Calendar OAuth2 Access Token (Azure AD v2.0 Endpoint).

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/outlookCalendar.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://graph.microsoft.com/v1.0/me/calendars", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

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

    // The HTTP request succeeded if the response status code = 200.
    if (resp.getStatusCode() != 200) {
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
        std.debug.print("Failed\n", .{});
        return;
    }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(try resp.getBodyStr(alloc)) catch {};
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Here is a sample response:

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //     "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#me/calendars",
    //     "value": [
    //         {
    //             "@odata.id": "https://graph.microsoft.com/v1.0/users('ddfcd489-628b-40d7-b48b-57002df800e5@1717622f-1d94-4d0c-9d74-709fad664b77')/calendars('AAMkAGI2TGuLAAA=')",
    //             "id": "AAMkAGI2TGuLAAA=",
    //             "name": "Calendar",
    //             "color": "auto",
    //             "changeKey": "nfZyf7VcrEKLNoU37KWlkQAAA0x0+w==",
    //             "canShare":true,
    //             "canViewPrivateItems":true,
    //             "hexColor": "",
    //             "canEdit":true,
    //             "allowedOnlineMeetingProviders": [
    //                 "teamsForBusiness"
    //             ],
    //             "defaultOnlineMeetingProvider": "teamsForBusiness",
    //             "isTallyingResponses": true,
    //             "isRemovable": false,
    //             "owner":{
    //                 "name":"Samantha Booth",
    //                 "address":"samanthab@adatum.onmicrosoft.com"
    //             }
    //         }
    //     ]
    // }

    var id: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var color: [:0]const u8 = "";
    var hex_color: [:0]const u8 = "";
    var is_default_calendar: bool = false;
    var change_key: [:0]const u8 = "";
    var can_share: bool = false;
    var can_view_private_items: bool = false;
    var can_edit: bool = false;
    var default_online_meeting_provider: [:0]const u8 = "";
    var is_tallying_responses: bool = false;
    var is_removable: bool = false;
    var owner_name: [:0]const u8 = "";
    var owner_address: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var str_val: [:0]const u8 = "";

    _ = try json.stringOf(alloc, "\"@odata.context\"");
    var i: i32 = 0;
    const count_i = json.sizeOfArray("value");
    while (i < count_i) {
        json.setI(i);
        id = try json.stringOf(alloc, "value[i].id");
        name = try json.stringOf(alloc, "value[i].name");
        color = try json.stringOf(alloc, "value[i].color");
        hex_color = try json.stringOf(alloc, "value[i].hexColor");
        is_default_calendar = json.boolOf("value[i].isDefaultCalendar");
        change_key = try json.stringOf(alloc, "value[i].changeKey");
        can_share = json.boolOf("value[i].canShare");
        can_view_private_items = json.boolOf("value[i].canViewPrivateItems");
        can_edit = json.boolOf("value[i].canEdit");
        default_online_meeting_provider = try json.stringOf(alloc, "value[i].defaultOnlineMeetingProvider");
        is_tallying_responses = json.boolOf("value[i].isTallyingResponses");
        is_removable = json.boolOf("value[i].isRemovable");
        owner_name = try json.stringOf(alloc, "value[i].owner.name");
        owner_address = try json.stringOf(alloc, "value[i].owner.address");
        j = 0;
        count_j = json.sizeOfArray("value[i].allowedOnlineMeetingProviders");
        while (j < count_j) {
            json.setJ(j);
            str_val = try json.stringOf(alloc, "value[i].allowedOnlineMeetingProviders[j]");
            j = j + 1;
        }

        i = i + 1;
    }
}