Sample code for 30+ languages & platforms
Zig

hacienda.go.cr Obtener Token

See more hacienda.go.cr Examples

Gets an access token using the Resource Owner Password Credential Grant for the Recepción de Comprobantes Electrónicos del Ministerio de Hacienda (Costa Rica)

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

    // Implements the following CURL command:

    // curl -X "POST" "https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token" \
    // -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
    // --data-urlencode "client_id=api-stag" \
    // --data-urlencode "username=cpj-3-101-261506@stag.comprobanteselectronicos.go.cr" \
    // --data-urlencode "password=my_password" \
    // --data-urlencode "grant_type=password"

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

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/auth/realms/rut-stag/protocol/openid-connect/token");
    req.setContentType("application/x-www-form-urlencoded");
    req.addParam("client_id", "api-stag");
    req.addParam("username", "cpj-3-101-261506@stag.comprobanteselectronicos.go.cr");
    req.addParam("password", "my_password");
    req.addParam("grant_type", "password");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token", req, 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)

    // {
    //   "access_token": "ey....",
    //   "expires_in": 300,
    //   "id_token": "ey....",
    //   "not-before-policy": 0,
    //   "refresh_expires_in": 1800,
    //   "refresh_token": "ey...",
    //   "session_state": "...",
    //   "token_type": "bearer"
    // }

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

    _ = try j_resp.stringOf(alloc, "access_token");
    _ = j_resp.intOf("expires_in");
    _ = try j_resp.stringOf(alloc, "id_token");
    _ = j_resp.intOf("not-before-policy");
    _ = j_resp.intOf("refresh_expires_in");
    _ = try j_resp.stringOf(alloc, "refresh_token");
    _ = try j_resp.stringOf(alloc, "session_state");
    _ = try j_resp.stringOf(alloc, "token_type");

    // Save the JSON to a file for future requests.
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    fac.writeEntireTextFile("qa_data/tokens/hacienda_cr.json", try j_resp.emit(alloc), "utf-8", false) catch {};
}