Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Get E-way Bill System Access TokenSends a request to get an E-way bill system access token.
#include <C_CkPublicKey.h> #include <C_CkRsa.h> #include <C_CkPrng.h> #include <C_CkJsonObject.h> #include <C_CkHttp.h> #include <C_CkHttpResponse.h> #include <C_CkStringBuilder.h> #include <C_CkCrypt2.h> #include <C_CkBinData.h> #include <C_CkFileAccess.h> void ChilkatSample(void) { HCkPublicKey pubkey; BOOL success; const char *password; HCkRsa rsa; const char *encPassword; HCkPrng prng; const char *app_key; const char *encAppKey; HCkJsonObject jsonBody; HCkHttp http; HCkHttpResponse resp; int respStatusCode; HCkJsonObject json; int status; HCkStringBuilder sbError; const char *authToken; HCkCrypt2 crypt; HCkBinData bdSek; HCkJsonObject jsonEwayAuth; HCkFileAccess fac; // 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 pubkey = CkPublicKey_Create(); success = CkPublicKey_LoadFromFile(pubkey,"qa_data/pem/eway_publickey.pem"); if (success != TRUE) { printf("%s\n",CkPublicKey_lastErrorText(pubkey)); CkPublicKey_Dispose(pubkey); return; } // Encrypt the password using the RSA public key provided by eway.. password = "my_wepgst_password"; rsa = CkRsa_Create(); CkRsa_putCharset(rsa,"utf-8"); CkRsa_putEncodingMode(rsa,"base64"); success = CkRsa_ImportPublicKeyObj(rsa,pubkey); if (success != TRUE) { printf("%s\n",CkRsa_lastErrorText(rsa)); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); return; } // Returns the encrypted password as base64 (because the EncodingMode = "base64") encPassword = CkRsa_encryptStringENC(rsa,password,FALSE); if (CkRsa_getLastMethodSuccess(rsa) != TRUE) { printf("%s\n",CkRsa_lastErrorText(rsa)); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); 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 = CkPrng_Create(); // Generate a random string containing some numbers, uppercase, and lowercase. app_key = CkPrng_randomString(prng,32,TRUE,TRUE,TRUE); printf("app_key = %s\n",app_key); // RSA encrypt the app_key. encAppKey = CkRsa_encryptStringENC(rsa,app_key,FALSE); if (CkRsa_getLastMethodSuccess(rsa) != TRUE) { printf("%s\n",CkRsa_lastErrorText(rsa)); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); CkPrng_Dispose(prng); return; } // Prepare the JSON body for the HTTP POST that gets the access token. jsonBody = CkJsonObject_Create(); CkJsonObject_UpdateString(jsonBody,"action","ACCESSTOKEN"); // Use your username instead of "09ABDC24212B1FK". CkJsonObject_UpdateString(jsonBody,"username","09ABDC24212B1FK"); CkJsonObject_UpdateString(jsonBody,"password",encPassword); CkJsonObject_UpdateString(jsonBody,"app_key",encAppKey); http = CkHttp_Create(); // Add required headers. // Use your ewb-user-id instead of "03AEXPR16A9M010" CkHttp_SetRequestHeader(http,"ewb-user-id","03AEXPR16A9M010"); // The Gstin should be the same as the username in the jsonBody above. CkHttp_SetRequestHeader(http,"Gstin","09ABDC24212B1FK"); CkHttp_putAccept(http,"application/json"); // POST the JSON... resp = CkHttp_PostJson2(http,"http://ewb.wepgst.com/api/Authenticate","application/json",CkJsonObject_emit(jsonBody)); if (CkHttp_getLastMethodSuccess(http) != TRUE) { printf("%s\n",CkHttp_lastErrorText(http)); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); CkPrng_Dispose(prng); CkJsonObject_Dispose(jsonBody); CkHttp_Dispose(http); return; } respStatusCode = CkHttpResponse_getStatusCode(resp); printf("response status code =%d\n",respStatusCode); printf("response body:\n"); printf("%s\n",CkHttpResponse_bodyStr(resp)); if (respStatusCode != 200) { CkHttpResponse_Dispose(resp); printf("Failed in some unknown way.\n"); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); CkPrng_Dispose(prng); CkJsonObject_Dispose(jsonBody); CkHttp_Dispose(http); 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. json = CkJsonObject_Create(); CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp)); CkHttpResponse_Dispose(resp); status = CkJsonObject_IntOf(json,"status"); printf("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"} sbError = CkStringBuilder_Create(); CkJsonObject_StringOfSb(json,"error",sbError); CkStringBuilder_Decode(sbError,"base64","utf-8"); printf("error: %s\n",CkStringBuilder_getAsString(sbError)); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); CkPrng_Dispose(prng); CkJsonObject_Dispose(jsonBody); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbError); return; } // At this point, we know the request was entirely successful. authToken = CkJsonObject_stringOf(json,"authtoken"); // Decrypt the sek key using our app_key. crypt = CkCrypt2_Create(); CkCrypt2_putCryptAlgorithm(crypt,"aes"); CkCrypt2_putCipherMode(crypt,"ecb"); CkCrypt2_putKeyLength(crypt,256); CkCrypt2_SetEncodedKey(crypt,app_key,"us-ascii"); CkCrypt2_putEncodingMode(crypt,"base64"); bdSek = CkBinData_Create(); CkBinData_AppendEncoded(bdSek,CkJsonObject_stringOf(json,"sek"),"base64"); CkCrypt2_DecryptBd(crypt,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. jsonEwayAuth = CkJsonObject_Create(); CkJsonObject_UpdateString(jsonEwayAuth,"authToken",authToken); CkJsonObject_UpdateString(jsonEwayAuth,"decryptedSek",CkBinData_getEncoded(bdSek,"base64")); CkJsonObject_putEmitCompact(jsonEwayAuth,FALSE); fac = CkFileAccess_Create(); CkFileAccess_WriteEntireTextFile(fac,"qa_data/tokens/ewayAuth.json",CkJsonObject_emit(jsonEwayAuth),"utf-8",FALSE); printf("Saved:\n"); printf("%s\n",CkJsonObject_emit(jsonEwayAuth)); // Sample output: // { // "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7", // "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho=" // } CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); CkPrng_Dispose(prng); CkJsonObject_Dispose(jsonBody); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbError); CkCrypt2_Dispose(crypt); CkBinData_Dispose(bdSek); CkJsonObject_Dispose(jsonEwayAuth); CkFileAccess_Dispose(fac); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.