Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkPublicKeyW.h>
#include <CkRsaW.h>
#include <CkPrngW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkCrypt2W.h>
#include <CkBinDataW.h>
#include <CkFileAccessW.h>
void ChilkatSample(void)
{
bool success = false;
// 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
CkPublicKeyW pubkey;
success = pubkey.LoadFromFile(L"qa_data/pem/eway_publickey.pem");
if (success == false) {
wprintf(L"%s\n",pubkey.lastErrorText());
return;
}
// Encrypt the password using the RSA public key provided by eway..
const wchar_t *password = L"my_wepgst_password";
CkRsaW rsa;
rsa.put_Charset(L"utf-8");
rsa.put_EncodingMode(L"base64");
success = rsa.UsePublicKey(pubkey);
if (success == false) {
wprintf(L"%s\n",rsa.lastErrorText());
return;
}
// Returns the encrypted password as base64 (because the EncodingMode = "base64")
const wchar_t *encPassword = rsa.encryptStringENC(password,false);
if (rsa.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",rsa.lastErrorText());
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.
CkPrngW prng;
// Generate a random string containing some numbers, uppercase, and lowercase.
const wchar_t *app_key = prng.randomString(32,true,true,true);
wprintf(L"app_key = %s\n",app_key);
// RSA encrypt the app_key.
const wchar_t *encAppKey = rsa.encryptStringENC(app_key,false);
if (rsa.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",rsa.lastErrorText());
return;
}
// Prepare the JSON body for the HTTP POST that gets the access token.
CkJsonObjectW jsonBody;
jsonBody.UpdateString(L"action",L"ACCESSTOKEN");
// Use your username instead of "09ABDC24212B1FK".
jsonBody.UpdateString(L"username",L"09ABDC24212B1FK");
jsonBody.UpdateString(L"password",encPassword);
jsonBody.UpdateString(L"app_key",encAppKey);
CkHttpW http;
// Add required headers.
// Use your ewb-user-id instead of "03AEXPR16A9M010"
http.SetRequestHeader(L"ewb-user-id",L"03AEXPR16A9M010");
// The Gstin should be the same as the username in the jsonBody above.
http.SetRequestHeader(L"Gstin",L"09ABDC24212B1FK");
http.put_Accept(L"application/json");
// POST the JSON...
CkHttpResponseW resp;
success = http.HttpJson(L"POST",L"http://ewb.wepgst.com/api/Authenticate",jsonBody,L"application/json",resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
int respStatusCode = resp.get_StatusCode();
wprintf(L"response status code =%d\n",respStatusCode);
wprintf(L"response body:\n");
wprintf(L"%s\n",resp.bodyStr());
if (respStatusCode != 200) {
wprintf(L"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.
CkJsonObjectW json;
json.Load(resp.bodyStr());
int status = json.IntOf(L"status");
wprintf(L"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"}
CkStringBuilderW sbError;
json.StringOfSb(L"error",sbError);
sbError.Decode(L"base64",L"utf-8");
wprintf(L"error: %s\n",sbError.getAsString());
return;
}
// At this point, we know the request was entirely successful.
const wchar_t *authToken = json.stringOf(L"authtoken");
// Decrypt the sek key using our app_key.
CkCrypt2W crypt;
crypt.put_CryptAlgorithm(L"aes");
crypt.put_CipherMode(L"ecb");
crypt.put_KeyLength(256);
crypt.SetEncodedKey(app_key,L"us-ascii");
crypt.put_EncodingMode(L"base64");
CkBinDataW bdSek;
bdSek.AppendEncoded(json.stringOf(L"sek"),L"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.
CkJsonObjectW jsonEwayAuth;
jsonEwayAuth.UpdateString(L"authToken",authToken);
jsonEwayAuth.UpdateString(L"decryptedSek",bdSek.getEncoded(L"base64"));
jsonEwayAuth.put_EmitCompact(false);
CkFileAccessW fac;
fac.WriteEntireTextFile(L"qa_data/tokens/ewayAuth.json",jsonEwayAuth.emit(),L"utf-8",false);
wprintf(L"Saved:\n");
wprintf(L"%s\n",jsonEwayAuth.emit());
// Sample output:
// {
// "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
// "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
//
}