Java Requires Chilkat v11.0.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Prng;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.Rsa;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// 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
PublicKey pubkey = new PublicKey();
try {
pubkey.loadFromFile("qa_data/pem/eway_publickey.pem");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Encrypt the password using the RSA public key provided by eway..
String password = "my_wepgst_password";
Rsa rsa = new Rsa();
rsa.setCharset("utf-8");
rsa.setEncodingMode("base64");
String encPassword;
try {
rsa.usePublicKey(pubkey);
// Returns the encrypted password as base64 (because the EncodingMode = "base64")
encPassword = rsa.encryptStringENC(password, false);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
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.
Prng prng = new Prng();
// Generate a random string containing some numbers, uppercase, and lowercase.
String app_key = prng.randomString(32, true, true, true);
System.out.println("app_key = " + app_key);
// RSA encrypt the app_key.
String encAppKey;
try {
encAppKey = rsa.encryptStringENC(app_key, false);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Prepare the JSON body for the HTTP POST that gets the access token.
JsonObject jsonBody = new JsonObject();
jsonBody.updateString("action", "ACCESSTOKEN");
// Use your username instead of "09ABDC24212B1FK".
jsonBody.updateString("username", "09ABDC24212B1FK");
jsonBody.updateString("password", encPassword);
jsonBody.updateString("app_key", encAppKey);
Http http = new Http();
// 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...
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", "http://ewb.wepgst.com/api/Authenticate", jsonBody, "application/json", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
int respStatusCode = resp.getStatusCode();
System.out.println("response status code =" + respStatusCode);
System.out.println("response body:");
System.out.println(resp.getBodyStr());
if (respStatusCode != 200) {
System.out.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.
JsonObject json = new JsonObject();
json.load(resp.getBodyStr());
int status = json.intOf("status");
System.out.println("status = " + status);
if (status != 1) {
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
StringBuilder sbError = new StringBuilder();
json.stringOfSb("error", sbError);
sbError.decode("base64", "utf-8");
System.out.println("error: " + sbError.getAsString());
return;
}
// At this point, we know the request was entirely successful.
String authToken = json.stringOf("authtoken");
// Decrypt the sek key using our app_key.
Crypt2 crypt = new Crypt2();
crypt.setCryptAlgorithm("aes");
crypt.setCipherMode("ecb");
crypt.setKeyLength(256);
crypt.setEncodedKey(app_key, "us-ascii");
crypt.setEncodingMode("base64");
BinData bdSek = new BinData();
bdSek.appendEncoded(json.stringOf("sek"), "base64");
crypt.decryptBd(bdSek);
// 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.
JsonObject jsonEwayAuth = new JsonObject();
jsonEwayAuth.updateString("authToken", authToken);
jsonEwayAuth.updateString("decryptedSek", bdSek.getEncoded("base64"));
jsonEwayAuth.setEmitCompact(false);
FileAccess fac = new FileAccess();
fac.writeEntireTextFile("qa_data/tokens/ewayAuth.json", jsonEwayAuth.emit(), "utf-8", false);
System.out.println("Saved:");
System.out.println(jsonEwayAuth.emit());
// Sample output:
// {
// "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
// "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
// }
}
}