Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPubkey
LOCAL cPassword
LOCAL oRsa
LOCAL cEncPassword
LOCAL oPrng
LOCAL cApp_key
LOCAL cEncAppKey
LOCAL oJsonBody
LOCAL oHttp
LOCAL oResp
LOCAL nRespStatusCode
LOCAL oJson
LOCAL nStatus
LOCAL oSbError
LOCAL cAuthToken
LOCAL oCrypt
LOCAL oBdSek
LOCAL oJsonEwayAuth
LOCAL oFac
nSuccess := 0
// 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
oPubkey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubkey:LoadFromFile("qa_data/pem/eway_publickey.pem")
IF (nSuccess == 0)
? oPubkey:LastErrorText
oPubkey:destroy()
RETURN
ENDIF
// Encrypt the password using the RSA public key provided by eway..
cPassword := "my_wepgst_password"
oRsa := CreateObject("Chilkat.Rsa")
oRsa:Charset := "utf-8"
oRsa:EncodingMode := "base64"
nSuccess := oRsa:UsePublicKey(oPubkey)
IF (nSuccess == 0)
? oRsa:LastErrorText
oPubkey:destroy()
oRsa:destroy()
RETURN
ENDIF
// Returns the encrypted password as base64 (because the EncodingMode = "base64")
cEncPassword := oRsa:EncryptStringENC(cPassword, 0)
IF (oRsa:LastMethodSuccess == 0)
? oRsa:LastErrorText
oPubkey:destroy()
oRsa:destroy()
RETURN
ENDIF
// 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.
oPrng := CreateObject("Chilkat.Prng")
// Generate a random string containing some numbers, uppercase, and lowercase.
cApp_key := oPrng:RandomString(32, 1, 1, 1)
? "app_key = " + cApp_key
// RSA encrypt the app_key.
cEncAppKey := oRsa:EncryptStringENC(cApp_key, 0)
IF (oRsa:LastMethodSuccess == 0)
? oRsa:LastErrorText
oPubkey:destroy()
oRsa:destroy()
oPrng:destroy()
RETURN
ENDIF
// Prepare the JSON body for the HTTP POST that gets the access token.
oJsonBody := CreateObject("Chilkat.JsonObject")
oJsonBody:UpdateString("action", "ACCESSTOKEN")
// Use your username instead of "09ABDC24212B1FK".
oJsonBody:UpdateString("username", "09ABDC24212B1FK")
oJsonBody:UpdateString("password", cEncPassword)
oJsonBody:UpdateString("app_key", cEncAppKey)
oHttp := CreateObject("Chilkat.Http")
// Add required headers.
// Use your ewb-user-id instead of "03AEXPR16A9M010"
oHttp:SetRequestHeader("ewb-user-id", "03AEXPR16A9M010")
// The Gstin should be the same as the username in the jsonBody above.
oHttp:SetRequestHeader("Gstin", "09ABDC24212B1FK")
oHttp:Accept := "application/json"
// POST the JSON...
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "http://ewb.wepgst.com/api/Authenticate", oJsonBody, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oPubkey:destroy()
oRsa:destroy()
oPrng:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
nRespStatusCode := oResp:StatusCode
? "response status code =" + Str(nRespStatusCode)
? "response body:"
? oResp:BodyStr
IF (nRespStatusCode != 200)
? "Failed in some unknown way."
oPubkey:destroy()
oRsa:destroy()
oPrng:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
// 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.
oJson := CreateObject("Chilkat.JsonObject")
oJson:Load(oResp:BodyStr)
nStatus := oJson:IntOf("status")
? "status = " + Str(nStatus)
IF (nStatus != 1)
// Failed. Base64 decode the error
// {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
// For an invalid password, the error is: {"errorCodes":"108"}
oSbError := CreateObject("Chilkat.StringBuilder")
oJson:StringOfSb("error", oSbError)
oSbError:Decode("base64", "utf-8")
? "error: " + oSbError:GetAsString()
oPubkey:destroy()
oRsa:destroy()
oPrng:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
oJson:destroy()
oSbError:destroy()
RETURN
ENDIF
// At this point, we know the request was entirely successful.
cAuthToken := oJson:StringOf("authtoken")
// Decrypt the sek key using our app_key.
oCrypt := CreateObject("Chilkat.Crypt2")
oCrypt:CryptAlgorithm := "aes"
oCrypt:CipherMode := "ecb"
oCrypt:KeyLength := 256
oCrypt:SetEncodedKey(cApp_key, "us-ascii")
oCrypt:EncodingMode := "base64"
oBdSek := CreateObject("Chilkat.BinData")
oBdSek:AppendEncoded(oJson:StringOf("sek"), "base64")
oCrypt:DecryptBd(oBdSek)
// 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.
oJsonEwayAuth := CreateObject("Chilkat.JsonObject")
oJsonEwayAuth:UpdateString("authToken", cAuthToken)
oJsonEwayAuth:UpdateString("decryptedSek", oBdSek:GetEncoded("base64"))
oJsonEwayAuth:EmitCompact := 0
oFac := CreateObject("Chilkat.FileAccess")
oFac:WriteEntireTextFile("qa_data/tokens/ewayAuth.json", oJsonEwayAuth:Emit(), "utf-8", 0)
? "Saved:"
? oJsonEwayAuth:Emit()
// Sample output:
// {
// "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
// "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
// }
oPubkey:destroy()
oRsa:destroy()
oPrng:destroy()
oJsonBody:destroy()
oHttp:destroy()
oResp:destroy()
oJson:destroy()
oSbError:destroy()
oCrypt:destroy()
oBdSek:destroy()
oJsonEwayAuth:destroy()
oFac:destroy()