Chilkat Examples

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

Excel Web API Examples

Primary Categories

ABN AMRO
AWS Secrets Manager
AWS Translate
Activix CRM
Adyen
Alibaba Cloud OSS
Amazon Cognito
Amazon DynamoDB
Amazon MWS
Amazon Pay
Amazon Rekognition
Amazon Voice ID
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Backblaze S3
Bitfinex v2 REST
Bluzone
BrickLink
CallRail
CardConnect
Cerved
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
Duo Auth MFA
ETrade
Ecwid
Egypt ITIDA
Etsy
Facebook
Faire
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Hungary NAV Invoicing
IBM Text to Speech
Ibanity
IntakeQ
Jira
Lightspeed
MYOB
Magento
Mailgun
Mastercard

MedTunnel
MercadoLibre
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Moody's
Okta OAuth/OIDC
OneLogin OIDC
OneNote
PRODA
PayPal
Paynow.pl
Peoplevox
Populi
QuickBooks
Rabobank
Refinitiv
Royal Mail OBA
SCiS Schools Catalogue
SII Chile
SMSAPI
SOAP finkok.com
SendGrid
Shippo
Shopify
Shopware
Shopware 6
SimpleTexting
Square
Stripe
SugarCRM
TicketBAI
Trello
Twilio
Twitter
UniPin
VoiceBase
Vonage
Walmart
Walmart v3
Wasabi
WhatsApp
WiX
WooCommerce
WordPress
Xero
Yahoo Mail
Yousign
Zoom
_Miscellaneous_
eBay
effectconnect
hacienda.go.cr

 

 

 

(Excel) ING Open Banking OAuth2 Client Credentials

Demonstrates how to get an access token for the ING Open Banking APIs using client credentials.

For more information, see https://developer.ing.com/openbanking/get-started/openbanking

Download Excel Class Modules

Chilkat Excel Class Modules

' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.

Dim cert As Chilkat.Cert
Set cert = Chilkat.NewCert

success = cert.LoadFromFile("qa_data/certs_and_keys/ING/example_client_tls.cer")
If (success = False) Then
    Debug.Print cert.LastErrorText
    Exit Sub
End If

Dim bdPrivKey As Chilkat.BinData
Set bdPrivKey = Chilkat.NewBinData
success = bdPrivKey.LoadFile("qa_data/certs_and_keys/ING/example_client_tls.key")
If (success = False) Then
    Debug.Print "Failed to load example_client_tls.key"
    Exit Sub
End If

' The OAuth 2.0 client_id for these certificates is e77d776b-90af-4684-bebc-521e5b2614dd. 
' Please note down this client_id since you will need it in the next steps to call the API.

Dim privKey As Chilkat.PrivateKey
Set privKey = Chilkat.NewPrivateKey
success = privKey.LoadAnyFormat(bdPrivKey,"")
If (success = False) Then
    Debug.Print privKey.LastErrorText
    Exit Sub
End If

' Associate the private key with the certificate.
success = cert.SetPrivateKey(privKey)
If (success = False) Then
    Debug.Print cert.LastErrorText
    Exit Sub
End If

Dim http As Chilkat.Http
Set http = Chilkat.NewHttp

success = http.SetSslClientCert(cert)
If (success = False) Then
    Debug.Print http.LastErrorText
    Exit Sub
End If

' Calculate the Digest and add the "Digest" header.  Do the equivalent of this:
' payload="grant_type=client_credentials"
' payloadDigest=`echo -n "$payload" | openssl dgst -binary -sha256 | openssl base64`
' digest=SHA-256=$payloadDigest
Dim crypt As Chilkat.Crypt2
Set crypt = Chilkat.NewCrypt2
crypt.HashAlgorithm = "SHA256"
crypt.EncodingMode = "base64"

payload = "grant_type=client_credentials"

payloadDigest = crypt.HashStringENC(payload)

' Calculate the current date/time and add the Date header.  
' reqDate=$(LC_TIME=en_US.UTF-8 date -u "+%a, %d %b %Y %H:%M:%S GMT")  
Dim dt As Chilkat.CkDateTime
Set dt = Chilkat.NewCkDateTime
Dim success As Boolean
success = dt.SetFromCurrentSystemTime()
' The desire date/time format is the "RFC822" format.
http.SetRequestHeader "Date",dt.GetAsRfc822(False)

' Calculate signature for signing your request
' Duplicate the following code:

' 	httpMethod="post"
' 	reqPath="/oauth2/token"
' 	signingString="(request-target): $httpMethod $reqPath
' 	date: $reqDate
' 	digest: $digest"
' 	signature=`printf "$signingString" | openssl dgst -sha256 -sign "${certPath}example_client_signing.key" -passin "pass:changeit" | openssl base64 -A`


httpMethod = "POST"

reqPath = "/oauth2/token"

Dim sbStringToSign As Chilkat.StringBuilder
Set sbStringToSign = Chilkat.NewStringBuilder
success = sbStringToSign.Append("(request-target): ")
success = sbStringToSign.Append(httpMethod)
success = sbStringToSign.ToLowercase()
success = sbStringToSign.Append(" ")
success = sbStringToSign.AppendLine(reqPath,False)

success = sbStringToSign.Append("date: ")
success = sbStringToSign.AppendLine(dt.GetAsRfc822(False),False)

success = sbStringToSign.Append("digest: SHA-256=")
success = sbStringToSign.Append(payloadDigest)

Dim signingPrivKey As Chilkat.PrivateKey
Set signingPrivKey = Chilkat.NewPrivateKey
success = signingPrivKey.LoadPemFile("qa_data/certs_and_keys/ING/example_client_signing.key")
If (success = False) Then
    Debug.Print signingPrivKey.LastErrorText
    Exit Sub
End If

Dim rsa As Chilkat.Rsa
Set rsa = Chilkat.NewRsa
success = rsa.ImportPrivateKeyObj(signingPrivKey)
If (success = False) Then
    Debug.Print rsa.LastErrorText
    Exit Sub
End If

rsa.EncodingMode = "base64"

b64Signature = rsa.SignStringENC(sbStringToSign.GetAsString(),"SHA256")

Dim sbAuthHdrVal As Chilkat.StringBuilder
Set sbAuthHdrVal = Chilkat.NewStringBuilder
success = sbAuthHdrVal.Append("Signature keyId=""e77d776b-90af-4684-bebc-521e5b2614dd"",")
success = sbAuthHdrVal.Append("algorithm=""rsa-sha256"",")
success = sbAuthHdrVal.Append("headers=""(request-target) date digest"",")
success = sbAuthHdrVal.Append("signature=""")
success = sbAuthHdrVal.Append(b64Signature)
success = sbAuthHdrVal.Append("""")

Dim sbDigestHdrVal As Chilkat.StringBuilder
Set sbDigestHdrVal = Chilkat.NewStringBuilder
success = sbDigestHdrVal.Append("SHA-256=")
success = sbDigestHdrVal.Append(payloadDigest)

' Do the following CURL statement:

' 	curl -i -X POST "${httpHost}${reqPath}" \
' 	-H 'Accept: application/json' \
' 	-H 'Content-Type: application/x-www-form-urlencoded' \
' 	-H "Digest: ${digest}" \
' 	-H "Date: ${reqDate}" \
' 	-H "authorization: Signature keyId=\"$keyId\",algorithm=\"rsa-sha256\",headers=\"(request-target) date digest\",signature=\"$signature\"" \
' 	-d "${payload}" \
' 	--cert "${certPath}tlsCert.crt" \
' 	--key "${certPath}tlsCert.key"

Dim req As Chilkat.HttpRequest
Set req = Chilkat.NewHttpRequest
req.AddParam "grant_type","client_credentials"
req.AddHeader "Accept","application/json"
req.AddHeader "Date",dt.GetAsRfc822(False)
req.AddHeader "Digest",sbDigestHdrVal.GetAsString()
req.AddHeader "Authorization",sbAuthHdrVal.GetAsString()

Set resp = http.PostUrlEncoded("https://api.sandbox.ing.com/oauth2/token",req)
If (http.LastMethodSuccess = False) Then
    Debug.Print http.LastErrorText
    Exit Sub
End If

' If successful, the status code = 200
Debug.Print "Response Status Code: "; resp.StatusCode
Debug.Print resp.BodyStr

Dim json As Chilkat.JsonObject
Set json = Chilkat.NewJsonObject
success = json.Load(resp.BodyStr)

json.EmitCompact = False
Debug.Print json.Emit()

' A successful response contains an access token such as:
' {
'   "access_token": "eyJhbGc ... bxI_SoPOBH9xmoM",
'   "expires_in": 905,
'   "scope": "payment-requests:view payment-requests:create payment-requests:close greetings:view virtual-ledger-accounts:fund-reservation:create virtual-ledger-accounts:fund-reservation:delete virtual-ledger-accounts:balance:view",
'   "token_type": "Bearer",
'   "keys": [
'     {
'       "kty": "RSA",
'       "n": "3l3rdz4...04VPkdV",
'       "e": "AQAB",
'       "use": "sig",
'       "alg": "RS256",
'       "x5t": "3c396700fc8cd709cf9cb5452a22bcde76985851"
'     }
'   ],
'   "client_id": "e77d776b-90af-4684-bebc-521e5b2614dd"
' }

' Use this online tool to generate parsing code from sample JSON: 
' Generate Parsing Code from JSON


access_token = json.StringOf("access_token")

expires_in = json.IntOf("expires_in")

scope = json.StringOf("scope")

token_type = json.StringOf("token_type")

client_id = json.StringOf("client_id")

i = 0

count_i = json.SizeOfArray("keys")
Do While i < count_i
    json.I = i
    kty = json.StringOf("keys[i].kty")
    n = json.StringOf("keys[i].n")
    e = json.StringOf("keys[i].e")
    use = json.StringOf("keys[i].use")
    alg = json.StringOf("keys[i].alg")
    x5t = json.StringOf("keys[i].x5t")
    i = i + 1
Loop

' This example will save the JSON containing the access key to a file so that
' a subsequent example can load it and then use the access key for a request, such as to create a payment request.
success = json.WriteFile("qa_data/tokens/ing_access_token.json")

 

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