Sample code for 30+ languages & platforms
Zig

Banco Inter OAuth2 Client Credentials

Generate an OAuth2 access token needed to consume the Inter APIs.

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

    // First load the certificate and private key, and set as the HTTP object's client certificate.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadFromFile("<nome arquivo certificado>.crt") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    const bd_priv_key = try chilkat.BinData.init();
    defer bd_priv_key.deinit();
    bd_priv_key.loadFile("<nome arquivo chave privada>.key") catch {
        std.debug.print("Failed to load <nome\n", .{});
        return;
    };

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadAnyFormat(bd_priv_key, "") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    cert.setPrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    http.setSslClientCert(cert) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/oauth/v2/token");
    req.setContentType("application/x-www-form-urlencoded");
    req.addParam("grant_type", "client_credentials");
    // Requested scopes in OAuth2 are typically SPACE separated.
    req.addParam("scope", "boleto-cobranca.read boleto-cobranca.write");
    req.addHeader("accept", "application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://cdpj.partners.bancointer.com.br/oauth/v2/token", req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    resp.getBodyJson(j_resp) 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;
    }

    j_resp.writeFile("qa_data/tokens/banco_inter_client_credentials.json") catch {
        std.debug.print("Failed to save JSON access token file.\n", .{});
        return;
    };

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