Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 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
    DECLARE @pubkey int
    EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubkey OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pubkey, 'LoadFromFile', @success OUT, 'qa_data/pem/eway_publickey.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pubkey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pubkey
        RETURN
      END

    -- Encrypt the password using the RSA public key provided by eway..
    DECLARE @password nvarchar(4000)
    SELECT @password = 'my_wepgst_password'
    DECLARE @rsa int
    EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT

    EXEC sp_OASetProperty @rsa, 'Charset', 'utf-8'
    EXEC sp_OASetProperty @rsa, 'EncodingMode', 'base64'

    EXEC sp_OAMethod @rsa, 'UsePublicKey', @success OUT, @pubkey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pubkey
        EXEC @hr = sp_OADestroy @rsa
        RETURN
      END

    -- Returns the encrypted password as base64 (because the EncodingMode = "base64")
    DECLARE @encPassword nvarchar(4000)
    EXEC sp_OAMethod @rsa, 'EncryptStringENC', @encPassword OUT, @password, 0
    EXEC sp_OAGetProperty @rsa, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pubkey
        EXEC @hr = sp_OADestroy @rsa
        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.
    DECLARE @prng int
    EXEC @hr = sp_OACreate 'Chilkat.Prng', @prng OUT

    -- Generate a random string containing some numbers, uppercase, and lowercase.
    DECLARE @app_key nvarchar(4000)
    EXEC sp_OAMethod @prng, 'RandomString', @app_key OUT, 32, 1, 1, 1


    PRINT 'app_key = ' + @app_key

    -- RSA encrypt the app_key.
    DECLARE @encAppKey nvarchar(4000)
    EXEC sp_OAMethod @rsa, 'EncryptStringENC', @encAppKey OUT, @app_key, 0
    EXEC sp_OAGetProperty @rsa, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pubkey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @prng
        RETURN
      END

    -- Prepare the JSON body for the HTTP POST that gets the access token.
    DECLARE @jsonBody int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonBody OUT

    EXEC sp_OAMethod @jsonBody, 'UpdateString', @success OUT, 'action', 'ACCESSTOKEN'
    -- Use your username instead of "09ABDC24212B1FK".
    EXEC sp_OAMethod @jsonBody, 'UpdateString', @success OUT, 'username', '09ABDC24212B1FK'
    EXEC sp_OAMethod @jsonBody, 'UpdateString', @success OUT, 'password', @encPassword
    EXEC sp_OAMethod @jsonBody, 'UpdateString', @success OUT, 'app_key', @encAppKey

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    -- Add required headers.
    -- Use your ewb-user-id instead of "03AEXPR16A9M010"
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'ewb-user-id', '03AEXPR16A9M010'
    -- The Gstin should be the same as the username in the jsonBody above.
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Gstin', '09ABDC24212B1FK'
    EXEC sp_OASetProperty @http, 'Accept', 'application/json'

    -- POST the JSON...
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'http://ewb.wepgst.com/api/Authenticate', @jsonBody, 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pubkey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @prng
        EXEC @hr = sp_OADestroy @jsonBody
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'response status code =' + @respStatusCode

    PRINT 'response body:'
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    IF @respStatusCode <> 200
      BEGIN

        PRINT 'Failed in some unknown way.'
        EXEC @hr = sp_OADestroy @pubkey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @prng
        EXEC @hr = sp_OADestroy @jsonBody
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        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.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0

    DECLARE @status int
    EXEC sp_OAMethod @json, 'IntOf', @status OUT, 'status'

    PRINT 'status = ' + @status

    IF @status <> 1
      BEGIN
        -- Failed.  Base64 decode the error
        -- {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
        -- For an invalid password, the error is: {"errorCodes":"108"}
        DECLARE @sbError int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbError OUT

        EXEC sp_OAMethod @json, 'StringOfSb', @success OUT, 'error', @sbError
        EXEC sp_OAMethod @sbError, 'Decode', @success OUT, 'base64', 'utf-8'

        EXEC sp_OAMethod @sbError, 'GetAsString', @sTmp0 OUT
        PRINT 'error: ' + @sTmp0
        EXEC @hr = sp_OADestroy @pubkey
        EXEC @hr = sp_OADestroy @rsa
        EXEC @hr = sp_OADestroy @prng
        EXEC @hr = sp_OADestroy @jsonBody
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbError
        RETURN
      END

    -- At this point, we know the request was entirely successful.
    DECLARE @authToken nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @authToken OUT, 'authtoken'

    -- Decrypt the sek key using our app_key.
    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'ecb'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 256
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @app_key, 'us-ascii'
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'

    DECLARE @bdSek int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdSek OUT

    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'sek'
    EXEC sp_OAMethod @bdSek, 'AppendEncoded', @success OUT, @sTmp0, 'base64'
    EXEC sp_OAMethod @crypt, 'DecryptBd', @success OUT, @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.
    DECLARE @jsonEwayAuth int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonEwayAuth OUT

    EXEC sp_OAMethod @jsonEwayAuth, 'UpdateString', @success OUT, 'authToken', @authToken
    EXEC sp_OAMethod @bdSek, 'GetEncoded', @sTmp0 OUT, 'base64'
    EXEC sp_OAMethod @jsonEwayAuth, 'UpdateString', @success OUT, 'decryptedSek', @sTmp0
    EXEC sp_OASetProperty @jsonEwayAuth, 'EmitCompact', 0

    DECLARE @fac int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    EXEC sp_OAMethod @jsonEwayAuth, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @fac, 'WriteEntireTextFile', @success OUT, 'qa_data/tokens/ewayAuth.json', @sTmp0, 'utf-8', 0


    PRINT 'Saved:'
    EXEC sp_OAMethod @jsonEwayAuth, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

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

    EXEC @hr = sp_OADestroy @pubkey
    EXEC @hr = sp_OADestroy @rsa
    EXEC @hr = sp_OADestroy @prng
    EXEC @hr = sp_OADestroy @jsonBody
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbError
    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @bdSek
    EXEC @hr = sp_OADestroy @jsonEwayAuth
    EXEC @hr = sp_OADestroy @fac


END
GO