Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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
local pubkey = chilkat.PublicKey.new()
local success = pubkey:load_from_file("qa_data/pem/eway_publickey.pem")
if not success then
    print(pubkey.last_error_text)
    return
end

-- Encrypt the password using the RSA public key provided by eway..
local password = "my_wepgst_password"
local rsa = chilkat.Rsa.new()
rsa.charset = "utf-8"
rsa.encoding_mode = "base64"

success = rsa:use_public_key(pubkey)
if not success then
    print(rsa.last_error_text)
    return
end

-- Returns the encrypted password as base64 (because the EncodingMode = "base64")
local encPassword = rsa:encrypt_string_enc(password, false)
if not rsa.last_method_success then
    print(rsa.last_error_text)
    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.
local prng = chilkat.Prng.new()
-- Generate a random string containing some numbers, uppercase, and lowercase.
local app_key = prng:random_string(32, true, true, true)

print("app_key = " .. app_key)

-- RSA encrypt the app_key.
local encAppKey = rsa:encrypt_string_enc(app_key, false)
if not rsa.last_method_success then
    print(rsa.last_error_text)
    return
end

-- Prepare the JSON body for the HTTP POST that gets the access token.
local jsonBody = chilkat.JsonObject.new()
jsonBody:update_string("action", "ACCESSTOKEN")
-- Use your username instead of "09ABDC24212B1FK".
jsonBody:update_string("username", "09ABDC24212B1FK")
jsonBody:update_string("password", encPassword)
jsonBody:update_string("app_key", encAppKey)

local http = chilkat.Http.new()

-- Add required headers.
-- Use your ewb-user-id instead of "03AEXPR16A9M010"
http:set_request_header("ewb-user-id", "03AEXPR16A9M010")
-- The Gstin should be the same as the username in the jsonBody above.
http:set_request_header("Gstin", "09ABDC24212B1FK")
http.accept = "application/json"

-- POST the JSON...
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "http://ewb.wepgst.com/api/Authenticate", jsonBody, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local respStatusCode = resp.status_code
print("response status code =" .. respStatusCode)
print("response body:")
print(resp.body_str)

if respStatusCode ~= 200 then
    print("Failed in some unknown way.")
    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.
local json = chilkat.JsonObject.new()
json:load(resp.body_str)

local status = json:int_of("status")
print("status = " .. status)

if status ~= 1 then
    -- Failed.  Base64 decode the error
    -- {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
    -- For an invalid password, the error is: {"errorCodes":"108"}
    local sbError = chilkat.StringBuilder.new()
    json:string_of_sb("error", sbError)
    sbError:decode("base64", "utf-8")
    print("error: " .. sbError:get_as_string())
    return
end

-- At this point, we know the request was entirely successful.
local authToken = json:string_of("authtoken")

-- Decrypt the sek key using our app_key.
local crypt = chilkat.Crypt2.new()
crypt.crypt_algorithm = "aes"
crypt.cipher_mode = "ecb"
crypt.key_length = 256
crypt:set_encoded_key(app_key, "us-ascii")
crypt.encoding_mode = "base64"

local bdSek = chilkat.BinData.new()
bdSek:append_encoded(json:string_of("sek"), "base64")
crypt:decrypt_bd(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.
local jsonEwayAuth = chilkat.JsonObject.new()
jsonEwayAuth:update_string("authToken", authToken)
jsonEwayAuth:update_string("decryptedSek", bdSek:get_encoded("base64"))
jsonEwayAuth.emit_compact = false

local fac = chilkat.FileAccess.new()
fac:write_entire_text_file("qa_data/tokens/ewayAuth.json", jsonEwayAuth:emit(), "utf-8", false)

print("Saved:")
print(jsonEwayAuth:emit())

-- Sample output:
-- {
--   "authToken": "IBTeFtxNfVurg71LTzZ2r0xK7",
--   "decryptedSek": "5g1TyTie7yoslU3DrbYATa7mWyPazlODE7cEh5Vy4Ho="
-- }