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
(SQL Server) Get E-way Bill System Access TokenSends a request to get an E-way bill system access token.
-- 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) -- 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 -- Use "Chilkat_9_5_0.PublicKey" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubkey OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @pubkey, 'LoadFromFile', @success OUT, 'qa_data/pem/eway_publickey.pem' IF @success <> 1 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 -- Use "Chilkat_9_5_0.Rsa" for versions of Chilkat < 10.0.0 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, 'ImportPublicKeyObj', @success OUT, @pubkey IF @success <> 1 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 <> 1 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 -- Use "Chilkat_9_5_0.Prng" for versions of Chilkat < 10.0.0 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 <> 1 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 -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 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 sp_OAMethod @jsonBody, 'Emit', @sTmp0 OUT EXEC sp_OAMethod @http, 'PostJson2', @resp OUT, 'http://ewb.wepgst.com/api/Authenticate', 'application/json', @sTmp0 EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 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 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 EXEC @hr = sp_OADestroy @resp 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 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 -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0 EXEC @hr = sp_OADestroy @resp 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 -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 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 @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 -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 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 @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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.