Zig
Zig
hacienda.go.cr Refrescar un Token
See more hacienda.go.cr Examples
Refreshes an access token for the Recepción de Comprobantes Electrónicos del Ministerio de Hacienda (Costa Rica)Chilkat Zig Downloads
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.
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// saved by this example: Get hacienda.co.cr OAuth2 Access Token
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/hacienda_cr.json") catch {
std.debug.print("Failed to load constantContact.json\n", .{});
return;
};
// The access token JSON looks like this:
// {
// "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"
// }
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 "refresh_token=ey..." \
// --data-urlencode "grant_type=refresh_token"
// 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("refresh_token", try json_token.stringOf(alloc, "refresh_token"));
req.addParam("grant_type", "refresh_token");
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 new JSON access token response to a file.
try j_resp.writeFile("qa_data/tokens/haciendda_cr.json");
}