Sample code for 30+ languages & platforms
Zig

Zoom API - Create JWT to Authenticate API Requests

See more Zoom Examples

Creates a JWT for the Zoom API.

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.

    // Use your API key and secret here...
    const api_key = "o9rw6Gq0RnqlkfaSqtCMOA";
    const api_secret = "UslmE23Kjh7at9z3If1xAHEyLmPDNxvxQrjR";

    // Create a JWT to authenticate Zoom API requests.
    const jwt = try chilkat.Jwt.init();
    defer jwt.deinit();

    const jose = try chilkat.JsonObject.init();
    defer jose.deinit();
    try jose.updateString("alg", "HS256");
    try jose.updateString("typ", "JWT");

    // Build claims to look like this:
    // {"aud":null,"iss":"o9rw6Gq0RnqlkfaSqtCMOA","exp":1627651762,"iat":1627646363}
    const claims = try chilkat.JsonObject.init();
    defer claims.deinit();
    try claims.updateString("iss", api_key);
    try claims.updateNull("aud");

    // Set the timestamp of when the JWT was created to now.
    const cur_date_time = jwt.genNumericDate(0);
    try claims.addIntAt(-1, "iat", cur_date_time);

    // Set the timestamp defining an expiration time (end time) for the token
    // to be now + 1 month(3600 * 24 * 30 seconds)
    const one_month = 3600 * 24 * 30;
    try claims.addIntAt(-1, "exp", cur_date_time + one_month);

    // Produce the smallest possible JWT:
    jwt.setAutoCompact(true);

    const str_jwt = try jwt.createJwt(alloc, try jose.emit(alloc), try claims.emit(alloc), api_secret);

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

    // Let's test the JWT to by sending the following request:

    // curl --request GET \
    //   --url 'https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1' \
    //   --header 'authorization: Bearer { your_token }' \
    //   --header 'content-type: application/json

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

    // Implements the following CURL command:

    // curl --request GET \
    //   --url 'https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1' \
    //   --header 'authorization: Bearer { your_token }' \
    //   --header 'content-type: application/json

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.setRequestHeader("content-type", "application/json");
    // Adds the "Authorization: Bearer { your_token }" header.
    http.setAuthToken(str_jwt);

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample output:

    // {
    //   "page_count": 1,
    //   "page_number": 1,
    //   "page_size": 30,
    //   "total_records": 1,
    //   "users": [
    //     {
    //       "id": "s8uAiMJiRmS_-eu1yOhKlg",
    //       "first_name": "Joe",
    //       "last_name": "Example",
    //       "email": "joe@example.com",
    //       "type": 1,
    //       "pmi": 5224934114,
    //       "timezone": "America/Chicago",
    //       "verified": 1,
    //       "created_at": "2021-07-30T11:56:37Z",
    //       "last_login_time": "2021-07-30T11:56:37Z",
    //       "language": "en-US",
    //       "phone_number": "",
    //       "status": "active",
    //       "role_id": "0"
    //     }
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var id: [:0]const u8 = "";
    var first_name: [:0]const u8 = "";
    var last_name: [:0]const u8 = "";
    var email: [:0]const u8 = "";
    var v_type: i32 = 0;
    var pmi: i32 = 0;
    var timezone: [:0]const u8 = "";
    var verified: i32 = 0;
    var created_at: [:0]const u8 = "";
    var last_login_time: [:0]const u8 = "";
    var language: [:0]const u8 = "";
    var phone_number: [:0]const u8 = "";
    var status: [:0]const u8 = "";
    var role_id: [:0]const u8 = "";

    _ = j_resp.intOf("page_count");
    _ = j_resp.intOf("page_number");
    _ = j_resp.intOf("page_size");
    _ = j_resp.intOf("total_records");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("users");
    while (i < count_i) {
        j_resp.setI(i);
        id = try j_resp.stringOf(alloc, "users[i].id");
        first_name = try j_resp.stringOf(alloc, "users[i].first_name");
        last_name = try j_resp.stringOf(alloc, "users[i].last_name");
        email = try j_resp.stringOf(alloc, "users[i].email");
        v_type = j_resp.intOf("users[i].type");
        pmi = j_resp.intOf("users[i].pmi");
        timezone = try j_resp.stringOf(alloc, "users[i].timezone");
        verified = j_resp.intOf("users[i].verified");
        created_at = try j_resp.stringOf(alloc, "users[i].created_at");
        last_login_time = try j_resp.stringOf(alloc, "users[i].last_login_time");
        language = try j_resp.stringOf(alloc, "users[i].language");
        phone_number = try j_resp.stringOf(alloc, "users[i].phone_number");
        status = try j_resp.stringOf(alloc, "users[i].status");
        role_id = try j_resp.stringOf(alloc, "users[i].role_id");
        i = i + 1;
    }
}