Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(DataFlex) Get E-way Bill System Access Token

Sends a request to get an E-way bill system access token.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Variant vPubkey
    Handle hoPubkey
    Boolean iSuccess
    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

    //  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 <> True) 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 ComImportPublicKeyObj Of hoRsa vPubkey To iSuccess
    If (iSuccess <> True) 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 <> True) 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 <> True) 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 ComEmit Of hoJsonBody To sTemp1
    Get ComPostJson2 Of hoHttp "http://ewb.wepgst.com/api/Authenticate" "application/json" sTemp1 To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) 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
        Send Destroy of hoResp
        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
    Send Destroy of hoResp

    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

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.