DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vPubkey
Handle hoPubkey
String sPassword
Handle hoRsa
String sEncPassword
Handle hoPrng
String sApp_key
String sEncAppKey
Body Handle hoJsonBody
Handle hoHttp
Variant vResp
Handle hoResp
Integer iRespStatusCode
Handle hoJson
Integer iStatus
Variant vSbError
Handle hoSbError
String sAuthToken
Handle hoCrypt
Variant vBdSek
Handle hoBdSek
EwayAuth Handle hoJsonEwayAuth
Handle hoFac
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPublicKey)) To hoPubkey
If (Not(IsComObjectCreated(hoPubkey))) Begin
Send CreateComObject of hoPubkey
End
Get ComLoadFromFile Of hoPubkey "qa_data/pem/eway_publickey.pem" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPubkey To sTemp1
Showln sTemp1
Procedure_Return
End
// Encrypt the password using the RSA public key provided by eway..
Move "my_wepgst_password" To sPassword
Get Create (RefClass(cComChilkatRsa)) To hoRsa
If (Not(IsComObjectCreated(hoRsa))) Begin
Send CreateComObject of hoRsa
End
Set ComCharset Of hoRsa To "utf-8"
Set ComEncodingMode Of hoRsa To "base64"
Get pvComObject of hoPubkey to vPubkey
Get ComUsePublicKey Of hoRsa vPubkey To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
// Returns the encrypted password as base64 (because the EncodingMode = "base64")
Get ComEncryptStringENC Of hoRsa sPassword False To sEncPassword
Get ComLastMethodSuccess Of hoRsa To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatPrng)) To hoPrng
If (Not(IsComObjectCreated(hoPrng))) Begin
Send CreateComObject of hoPrng
End
// Generate a random string containing some numbers, uppercase, and lowercase.
Get ComRandomString Of hoPrng 32 True True True To sApp_key
Showln "app_key = " sApp_key
// RSA encrypt the app_key.
Get ComEncryptStringENC Of hoRsa sApp_key False To sEncAppKey
Get ComLastMethodSuccess Of hoRsa To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
// Prepare the JSON body for the HTTP POST that gets the access token.
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonBody
If (Not(IsComObjectCreated(hoJsonBody))) Begin
Send CreateComObject of hoJsonBody
End
Get ComUpdateString Of hoJsonBody "action" "ACCESSTOKEN" To iSuccess
// Use your username instead of "09ABDC24212B1FK".
Get ComUpdateString Of hoJsonBody "username" "09ABDC24212B1FK" To iSuccess
Get ComUpdateString Of hoJsonBody "password" sEncPassword To iSuccess
Get ComUpdateString Of hoJsonBody "app_key" sEncAppKey To iSuccess
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// Add required headers.
// Use your ewb-user-id instead of "03AEXPR16A9M010"
Send ComSetRequestHeader To hoHttp "ewb-user-id" "03AEXPR16A9M010"
// The Gstin should be the same as the username in the jsonBody above.
Send ComSetRequestHeader To hoHttp "Gstin" "09ABDC24212B1FK"
Set ComAccept Of hoHttp To "application/json"
// POST the JSON...
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoJsonBody to vJsonBody
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" "http://ewb.wepgst.com/api/Authenticate" vJsonBody "application/json" vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStatusCode Of hoResp To iRespStatusCode
Showln "response status code =" iRespStatusCode
Showln "response body:"
Get ComBodyStr Of hoResp To sTemp1
Showln sTemp1
If (iRespStatusCode <> 200) Begin
Showln "Failed in some unknown way."
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoJson sTemp1 To iSuccess
Get ComIntOf Of hoJson "status" To iStatus
Showln "status = " iStatus
If (iStatus <> 1) Begin
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbError
If (Not(IsComObjectCreated(hoSbError))) Begin
Send CreateComObject of hoSbError
End
Get pvComObject of hoSbError to vSbError
Get ComStringOfSb Of hoJson "error" vSbError To iSuccess
Get ComDecode Of hoSbError "base64" "utf-8" To iSuccess
Get ComGetAsString Of hoSbError To sTemp1
Showln "error: " sTemp1
Procedure_Return
End
// At this point, we know the request was entirely successful.
Get ComStringOf Of hoJson "authtoken" To sAuthToken
// Decrypt the sek key using our app_key.
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Set ComCryptAlgorithm Of hoCrypt To "aes"
Set ComCipherMode Of hoCrypt To "ecb"
Set ComKeyLength Of hoCrypt To 256
Send ComSetEncodedKey To hoCrypt sApp_key "us-ascii"
Set ComEncodingMode Of hoCrypt To "base64"
Get Create (RefClass(cComChilkatBinData)) To hoBdSek
If (Not(IsComObjectCreated(hoBdSek))) Begin
Send CreateComObject of hoBdSek
End
Get ComStringOf Of hoJson "sek" To sTemp1
Get ComAppendEncoded Of hoBdSek sTemp1 "base64" To iSuccess
Get pvComObject of hoBdSek to vBdSek
Get ComDecryptBd Of hoCrypt vBdSek To iSuccess
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonEwayAuth
If (Not(IsComObjectCreated(hoJsonEwayAuth))) Begin
Send CreateComObject of hoJsonEwayAuth
End
Get ComUpdateString Of hoJsonEwayAuth "authToken" sAuthToken To iSuccess
Get ComGetEncoded Of hoBdSek "base64" To sTemp1
Get ComUpdateString Of hoJsonEwayAuth "decryptedSek" sTemp1 To iSuccess
Set ComEmitCompact Of hoJsonEwayAuth To False
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComEmit Of hoJsonEwayAuth To sTemp1
Get ComWriteEntireTextFile Of hoFac "qa_data/tokens/ewayAuth.json" sTemp1 "utf-8" False To iSuccess
Showln "Saved:"
Get ComEmit Of hoJsonEwayAuth To sTemp1
Showln sTemp1
// Sample output:
// {
// "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
// "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
//
End_Procedure