Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Tcl Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Tcl) Get E-way Bill System Access Token

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

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

# 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
set pubkey [new_CkPublicKey]

set success [CkPublicKey_LoadFromFile $pubkey "qa_data/pem/eway_publickey.pem"]
if {$success != 1} then {
    puts [CkPublicKey_lastErrorText $pubkey]
    delete_CkPublicKey $pubkey
    exit
}

# Encrypt the password using the RSA public key provided by eway..
set password "my_wepgst_password"
set rsa [new_CkRsa]

CkRsa_put_Charset $rsa "utf-8"
CkRsa_put_EncodingMode $rsa "base64"

set success [CkRsa_ImportPublicKeyObj $rsa $pubkey]
if {$success != 1} then {
    puts [CkRsa_lastErrorText $rsa]
    delete_CkPublicKey $pubkey
    delete_CkRsa $rsa
    exit
}

# Returns the encrypted password as base64 (because the EncodingMode = "base64")
set encPassword [CkRsa_encryptStringENC $rsa $password 0]
if {[CkRsa_get_LastMethodSuccess $rsa] != 1} then {
    puts [CkRsa_lastErrorText $rsa]
    delete_CkPublicKey $pubkey
    delete_CkRsa $rsa
    exit
}

# 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.
set prng [new_CkPrng]

# Generate a random string containing some numbers, uppercase, and lowercase.
set app_key [CkPrng_randomString $prng 32 1 1 1]

puts "app_key = $app_key"

# RSA encrypt the app_key.
set encAppKey [CkRsa_encryptStringENC $rsa $app_key 0]
if {[CkRsa_get_LastMethodSuccess $rsa] != 1} then {
    puts [CkRsa_lastErrorText $rsa]
    delete_CkPublicKey $pubkey
    delete_CkRsa $rsa
    delete_CkPrng $prng
    exit
}

# Prepare the JSON body for the HTTP POST that gets the access token.
set jsonBody [new_CkJsonObject]

CkJsonObject_UpdateString $jsonBody "action" "ACCESSTOKEN"
# Use your username instead of "09ABDC24212B1FK".
CkJsonObject_UpdateString $jsonBody "username" "09ABDC24212B1FK"
CkJsonObject_UpdateString $jsonBody "password" $encPassword
CkJsonObject_UpdateString $jsonBody "app_key" $encAppKey

set http [new_CkHttp]

# Add required headers.
# Use your ewb-user-id instead of "03AEXPR16A9M010"
CkHttp_SetRequestHeader $http "ewb-user-id" "03AEXPR16A9M010"
# The Gstin should be the same as the username in the jsonBody above.
CkHttp_SetRequestHeader $http "Gstin" "09ABDC24212B1FK"
CkHttp_put_Accept $http "application/json"

# POST the JSON...
# resp is a CkHttpResponse
set resp [CkHttp_PostJson2 $http "http://ewb.wepgst.com/api/Authenticate" "application/json" [CkJsonObject_emit $jsonBody]]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkPublicKey $pubkey
    delete_CkRsa $rsa
    delete_CkPrng $prng
    delete_CkJsonObject $jsonBody
    delete_CkHttp $http
    exit
}

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "response status code =$respStatusCode"
puts "response body:"
puts [CkHttpResponse_bodyStr $resp]

if {$respStatusCode != 200} then {
    delete_CkHttpResponse $resp

    puts "Failed in some unknown way."
    delete_CkPublicKey $pubkey
    delete_CkRsa $rsa
    delete_CkPrng $prng
    delete_CkJsonObject $jsonBody
    delete_CkHttp $http
    exit
}

# 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.
set json [new_CkJsonObject]

CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]
delete_CkHttpResponse $resp

set status [CkJsonObject_IntOf $json "status"]
puts "status = $status"

if {$status != 1} then {
    # Failed.  Base64 decode the error
    # {"status":"0","error":"eyJlcnJvckNvZGVzIjoiMTA4In0="}
    # For an invalid password, the error is: {"errorCodes":"108"}
    set sbError [new_CkStringBuilder]

    CkJsonObject_StringOfSb $json "error" $sbError
    CkStringBuilder_Decode $sbError "base64" "utf-8"
    puts "error: [CkStringBuilder_getAsString $sbError]"
    delete_CkPublicKey $pubkey
    delete_CkRsa $rsa
    delete_CkPrng $prng
    delete_CkJsonObject $jsonBody
    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkStringBuilder $sbError
    exit
}

# At this point, we know the request was entirely successful.
set authToken [CkJsonObject_stringOf $json "authtoken"]

# Decrypt the sek key using our app_key.
set crypt [new_CkCrypt2]

CkCrypt2_put_CryptAlgorithm $crypt "aes"
CkCrypt2_put_CipherMode $crypt "ecb"
CkCrypt2_put_KeyLength $crypt 256
CkCrypt2_SetEncodedKey $crypt $app_key "us-ascii"
CkCrypt2_put_EncodingMode $crypt "base64"

set bdSek [new_CkBinData]

CkBinData_AppendEncoded $bdSek [CkJsonObject_stringOf $json "sek"] "base64"
CkCrypt2_DecryptBd $crypt $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.
set jsonEwayAuth [new_CkJsonObject]

CkJsonObject_UpdateString $jsonEwayAuth "authToken" $authToken
CkJsonObject_UpdateString $jsonEwayAuth "decryptedSek" [CkBinData_getEncoded $bdSek "base64"]
CkJsonObject_put_EmitCompact $jsonEwayAuth 0

set fac [new_CkFileAccess]

CkFileAccess_WriteEntireTextFile $fac "qa_data/tokens/ewayAuth.json" [CkJsonObject_emit $jsonEwayAuth] "utf-8" 0

puts "Saved:"
puts [CkJsonObject_emit $jsonEwayAuth]

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

delete_CkPublicKey $pubkey
delete_CkRsa $rsa
delete_CkPrng $prng
delete_CkJsonObject $jsonBody
delete_CkHttp $http
delete_CkJsonObject $json
delete_CkStringBuilder $sbError
delete_CkCrypt2 $crypt
delete_CkBinData $bdSek
delete_CkJsonObject $jsonEwayAuth
delete_CkFileAccess $fac

 

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