Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// 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
let pubkey = chilkat::PublicKey::new();
if pubkey.load_from_file("qa_data/pem/eway_publickey.pem").is_err() {
println!("{}", pubkey.last_error_text());
return;
}
// Encrypt the password using the RSA public key provided by eway..
let password = "my_wepgst_password".to_string();
let rsa = chilkat::Rsa::new();
rsa.set_charset("utf-8");
rsa.set_encoding_mode("base64");
if rsa.use_public_key(&pubkey).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// Returns the encrypted password as base64 (because the EncodingMode = "base64")
let Ok(enc_password) = rsa.encrypt_string_enc(&password, false) else {
println!("{}", rsa.last_error_text());
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.
let prng = chilkat::Prng::new();
// Generate a random string containing some numbers, uppercase, and lowercase.
let app_key = prng.random_string(32, true, true, true).unwrap_or_default();
println!("app_key = {}", app_key);
// RSA encrypt the app_key.
let Ok(enc_app_key) = rsa.encrypt_string_enc(&app_key, false) else {
println!("{}", rsa.last_error_text());
return;
};
// Prepare the JSON body for the HTTP POST that gets the access token.
let json_body = chilkat::JsonObject::new();
let _ = json_body.update_string("action", "ACCESSTOKEN");
// Use your username instead of "09ABDC24212B1FK".
let _ = json_body.update_string("username", "09ABDC24212B1FK");
let _ = json_body.update_string("password", &enc_password);
let _ = json_body.update_string("app_key", &enc_app_key);
let http = chilkat::Http::new();
// Add required headers.
// Use your ewb-user-id instead of "03AEXPR16A9M010"
http.set_request_header("ewb-user-id", "03AEXPR16A9M010");
// The Gstin should be the same as the username in the jsonBody above.
http.set_request_header("Gstin", "09ABDC24212B1FK");
http.set_accept("application/json");
// POST the JSON...
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "http://ewb.wepgst.com/api/Authenticate", &json_body, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let resp_status_code = resp.status_code();
println!("response status code ={}", resp_status_code);
println!("response body:");
println!("{}", resp.body_str());
if resp_status_code != 200 {
println!("Failed in some unknown way.");
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.
let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str());
let status = json.int_of("status");
println!("status = {}", status);
if status != 1 {
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
let sb_error = chilkat::StringBuilder::new();
let _ = json.string_of_sb("error", &sb_error);
let _ = sb_error.decode("base64", "utf-8");
println!("error: {}", sb_error.get_as_string().unwrap_or_default());
return;
}
// At this point, we know the request was entirely successful.
let auth_token = json.string_of("authtoken").unwrap_or_default();
// Decrypt the sek key using our app_key.
let crypt = chilkat::Crypt2::new();
crypt.set_crypt_algorithm("aes");
crypt.set_cipher_mode("ecb");
crypt.set_key_length(256);
crypt.set_encoded_key(&app_key, "us-ascii");
crypt.set_encoding_mode("base64");
let bd_sek = chilkat::BinData::new();
let _ = bd_sek.append_encoded(&json.string_of("sek").unwrap_or_default(), "base64");
let _ = crypt.decrypt_bd(&bd_sek);
// 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.
let json_eway_auth = chilkat::JsonObject::new();
let _ = json_eway_auth.update_string("authToken", &auth_token);
let _ = json_eway_auth.update_string("decryptedSek", &bd_sek.get_encoded("base64").unwrap_or_default());
json_eway_auth.set_emit_compact(false);
let fac = chilkat::FileAccess::new();
let _ = fac.write_entire_text_file("qa_data/tokens/ewayAuth.json", &json_eway_auth.emit().unwrap_or_default(), "utf-8", false);
println!("Saved:");
println!("{}", json_eway_auth.emit().unwrap_or_default());
// Sample output:
// {
// "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
// "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
// }