Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

MS Graph Create Calendar

See more Microsoft Calendar Examples

Creates a new calendar.

For more details, see https://docs.microsoft.com/en-us/graph/api/user-post-calendars?view=graph-rest-1.0

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 as shown here:
    //    Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

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

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

    // Create a JSON body for the HTTP POST
    // {
    //   "name": "Work"
    // }
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("name", "Work") catch {};

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

    json.load(try resp.getBodyStr(alloc)) catch {};
    json.setEmitCompact(false);

    if (resp.getStatusCode() != 201) {
        std.debug.print("{s}\n", .{try json.emit(alloc)});
        std.debug.print("Failed, response status code = {d}\n", .{resp.getStatusCode()});
        return;
    }

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // A sample response:
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/calendars/$entity",
    //   "id": "AQMkAD...TgAAAA==",
    //   "name": "Work",
    //   "color": "auto",
    //   "changeKey": "5+vF7T...HjDcA==",
    //   "canShare": true,
    //   "canViewPrivateItems": true,
    //   "canEdit": true,
    //   "owner": {
    //     "name": "...",
    //     "address": "outlook_3A33...4CC15@outlook.com"
    //   }
    // }

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

    _ = try json.stringOf(alloc, "\"@odata.context\"");
    _ = try json.stringOf(alloc, "id");
    _ = try json.stringOf(alloc, "name");
    _ = try json.stringOf(alloc, "color");
    _ = try json.stringOf(alloc, "changeKey");
    _ = json.boolOf("canShare");
    _ = json.boolOf("canViewPrivateItems");
    _ = json.boolOf("canEdit");
    _ = try json.stringOf(alloc, "owner.name");
    _ = try json.stringOf(alloc, "owner.address");

    std.debug.print("Success.\n", .{});
}