Zig Requires Chilkat v11.0.0+
Zig
Get E-way Bill System Access Token
See more HTTP Misc Examples
Sends a request to get an E-way bill system access token.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First load the public key provided by the E-way bill System
const pubkey = try chilkat.PublicKey.init();
defer pubkey.deinit();
pubkey.loadFromFile("qa_data/pem/eway_publickey.pem") catch {
std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
return;
};
// Encrypt the password using the RSA public key provided by eway..
const password = "my_wepgst_password";
const rsa = try chilkat.Rsa.init();
defer rsa.deinit();
rsa.setCharset("utf-8");
rsa.setEncodingMode("base64");
rsa.usePublicKey(pubkey) catch {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
};
// Returns the encrypted password as base64 (because the EncodingMode = "base64")
const enc_password = rsa.encryptStringENC(alloc, password, false) catch {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
};
// Generate a random app_key. This should be 32 bytes (us-ascii chars)
// We need 32 bytes because we'll be doing 256-bit AES ECB encryption, and 32 bytes = 256 bits.
const prng = try chilkat.Prng.init();
defer prng.deinit();
// Generate a random string containing some numbers, uppercase, and lowercase.
const app_key = try prng.randomString(alloc, 32, true, true, true);
std.debug.print("app_key = {s}\n", .{app_key});
// RSA encrypt the app_key.
const enc_app_key = rsa.encryptStringENC(alloc, app_key, false) catch {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
};
// Prepare the JSON body for the HTTP POST that gets the access token.
const json_body = try chilkat.JsonObject.init();
defer json_body.deinit();
json_body.updateString("action", "ACCESSTOKEN") catch {};
// Use your username instead of "09ABDC24212B1FK".
json_body.updateString("username", "09ABDC24212B1FK") catch {};
json_body.updateString("password", enc_password) catch {};
json_body.updateString("app_key", enc_app_key) catch {};
const http = try chilkat.Http.init();
defer http.deinit();
// Add required headers.
// Use your ewb-user-id instead of "03AEXPR16A9M010"
http.setRequestHeader("ewb-user-id", "03AEXPR16A9M010");
// The Gstin should be the same as the username in the jsonBody above.
http.setRequestHeader("Gstin", "09ABDC24212B1FK");
http.setAccept("application/json");
// POST the JSON...
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "http://ewb.wepgst.com/api/Authenticate", json_body, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const resp_status_code = resp.getStatusCode();
std.debug.print("response status code ={d}\n", .{resp_status_code});
std.debug.print("response body:\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
if (resp_status_code != 200) {
std.debug.print("Failed in some unknown way.\n", .{});
return;
}
// When the response status code = 200, we'll have either
// success response like this:
// {"status":"1","authtoken":"...","sek":"..."}
//
// or a failed response like this:
//
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// Load the response body into a JSON object.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(try resp.getBodyStr(alloc)) catch {};
const status = json.intOf("status");
std.debug.print("status = {d}\n", .{status});
if (status != 1) {
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
const sb_error = try chilkat.StringBuilder.init();
defer sb_error.deinit();
json.stringOfSb("error", sb_error) catch {};
sb_error.decode("base64", "utf-8") catch {};
std.debug.print("error: {s}\n", .{try sb_error.getAsString(alloc)});
return;
}
// At this point, we know the request was entirely successful.
const auth_token = try json.stringOf(alloc, "authtoken");
// Decrypt the sek key using our app_key.
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setCryptAlgorithm("aes");
crypt.setCipherMode("ecb");
crypt.setKeyLength(256);
crypt.setEncodedKey(app_key, "us-ascii");
crypt.setEncodingMode("base64");
const bd_sek = try chilkat.BinData.init();
defer bd_sek.deinit();
bd_sek.appendEncoded(try json.stringOf(alloc, "sek"), "base64") catch {};
crypt.decryptBd(bd_sek) catch {};
// bdSek now contains the decrypted symmetric encryption key...
// We'll use it to encrypt the JSON payloads we send.
// Let's persist our authtoken and decrypted sek (symmetric encryption key).
// To send EWAY requests (such as to create an e-way bill), we'll just load
// and use these pre-obtained credentials.
const json_eway_auth = try chilkat.JsonObject.init();
defer json_eway_auth.deinit();
json_eway_auth.updateString("authToken", auth_token) catch {};
json_eway_auth.updateString("decryptedSek", try bd_sek.getEncoded(alloc, "base64")) catch {};
json_eway_auth.setEmitCompact(false);
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
fac.writeEntireTextFile("qa_data/tokens/ewayAuth.json", try json_eway_auth.emit(alloc), "utf-8", false) catch {};
std.debug.print("Saved:\n", .{});
std.debug.print("{s}\n", .{try json_eway_auth.emit(alloc)});
// Sample output:
// {
// "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
// "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
// }
}