Sample code for 30+ languages & platforms
Zig

RSAP Union API - Get OAuth2 Access Token

See more _Miscellaneous_ Examples

Demonstrates how to get an OAuth2 access token for the RSAP Union API. Note: This uses the client credentials flow, which does NOT require an interactive engagement using a browser.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // The following JSON is sent in the request body.

    // {
    //   "grant_type": "client_credentials",
    //   "client_id": 1234,
    //   "client_secret": "23456abcde"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("grant_type", "client_credentials") catch {};
    json.updateInt("client_id", 1234) catch {};
    json.updateString("client_secret", "23456abcde") catch {};

    http.setRequestHeader("Content-type", "application/json");

    // Add the client certificate TLS authentication.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadFromFile("qa_data/certs_and_keys/union_client_certificate.crt") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadAnyFormatFile("qa_data/certs_and_keys/union_client_certificate.nopass.key", "") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    // Associate the private key with the cert.
    // This will fail if the private key is not actually the correct one that corresponds to the public key stored within the cert.
    cert.setPrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Tell HTTP to use the cert for client TLS certificate authentication.
    http.setSslClientCert(cert) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api-test.rsap.ca/oauth/token", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    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 = resp.getStatusCode();
    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 resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "token_type": "Bearer",
    //   "expires_in": 3600,
    //   "access_token": "eyJ0eXAi...LnE"
    // }

    // This token expires in 1 hour.  Your application could re-use the same token for up to an hour,
    // or it can simply get a new access token before each request (if you're not doing too many requests).
    try j_resp.writeFile("qa_data/tokens/rsapToken.json");
}