B4X Requires Chilkat v11.0.0+
B4X
ING Open Banking OAuth2 Client Credentials
See more OAuth2 Examples
Demonstrates how to get an access token for the ING Open Banking APIs using client credentials.Chilkat B4X Downloads
Dim success As Boolean = False
' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadFromFile("qa_data/certs_and_keys/ING/example_client_tls.cer")
If success = False Then
Log(cert.LastErrorText)
Return
End If
Dim bdPrivKey As ChilkatBinData
bdPrivKey.Initialize
success = bdPrivKey.LoadFile("qa_data/certs_and_keys/ING/example_client_tls.key")
If success = False Then
Log("Failed to load example_client_tls.key")
Return
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 ChilkatPrivateKey
privKey.Initialize("privKey")
success = privKey.LoadAnyFormat(bdPrivKey, "")
If success = False Then
Log(privKey.LastErrorText)
Return
End If
' Associate the private key with the certificate.
success = cert.SetPrivateKey(privKey)
If success = False Then
Log(cert.LastErrorText)
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
success = http.SetSslClientCert(cert)
If success = False Then
Log(http.LastErrorText)
Return
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 ChilkatCrypt2
crypt.Initialize("crypt")
crypt.HashAlgorithm = "SHA256"
crypt.EncodingMode = "base64"
Dim payload As String = "grant_type=client_credentials"
Dim payloadDigest As String = 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 ChilkatDateTime
dt.Initialize
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`
Dim httpMethod As String = "POST"
Dim reqPath As String = "/oauth2/token"
Dim sbStringToSign As ChilkatStringBuilder
sbStringToSign.Initialize
sbStringToSign.Append("(request-target): ")
sbStringToSign.Append(httpMethod)
sbStringToSign.ToLowercase
sbStringToSign.Append(" ")
sbStringToSign.AppendLine(reqPath, False)
sbStringToSign.Append("date: ")
sbStringToSign.AppendLine(dt.GetAsRfc822(False), False)
sbStringToSign.Append("digest: SHA-256=")
sbStringToSign.Append(payloadDigest)
Dim signingPrivKey As ChilkatPrivateKey
signingPrivKey.Initialize("signingPrivKey")
success = signingPrivKey.LoadPemFile("qa_data/certs_and_keys/ING/example_client_signing.key")
If success = False Then
Log(signingPrivKey.LastErrorText)
Return
End If
Dim rsa As ChilkatRsa
rsa.Initialize
success = rsa.UsePrivateKey(signingPrivKey)
If success = False Then
Log(rsa.LastErrorText)
Return
End If
rsa.EncodingMode = "base64"
Dim b64Signature As String = rsa.SignStringENC(sbStringToSign.GetAsString, "SHA256")
Dim sbAuthHdrVal As ChilkatStringBuilder
sbAuthHdrVal.Initialize
sbAuthHdrVal.Append("Signature keyId=" & Chr(34) & "e77d776b-90af-4684-bebc-521e5b2614dd" & Chr(34) & ",")
sbAuthHdrVal.Append("algorithm=" & Chr(34) & "rsa-sha256" & Chr(34) & ",")
sbAuthHdrVal.Append("headers=" & Chr(34) & "(request-target) date digest" & Chr(34) & ",")
sbAuthHdrVal.Append("signature=" & Chr(34))
sbAuthHdrVal.Append(b64Signature)
sbAuthHdrVal.Append(Chr(34))
Dim sbDigestHdrVal As ChilkatStringBuilder
sbDigestHdrVal.Initialize
sbDigestHdrVal.Append("SHA-256=")
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 ChilkatHttpRequest
req.Initialize
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)
req.HttpVerb = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim resp As ChilkatHttpResponse
resp.Initialize
success = http.HttpReq("https://api.sandbox.ing.com/oauth2/token", req, resp)
If success = False Then
Log(http.LastErrorText)
Return
End If
' If successful, the status code = 200
Log("Response Status Code: " & resp.StatusCode)
Log(resp.BodyStr)
Dim json As ChilkatJsonObject
json.Initialize
json.Load(resp.BodyStr)
json.EmitCompact = False
Log(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
Dim kty As String
Dim n As String
Dim e As String
Dim use As String
Dim alg As String
Dim x5t As String
Dim access_token As String = json.StringOf("access_token")
Dim expires_in As Int = json.IntOf("expires_in")
Dim scope As String = json.StringOf("scope")
Dim token_type As String = json.StringOf("token_type")
Dim client_id As String = json.StringOf("client_id")
Dim i As Int = 0
Dim count_i As Int = 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.
json.WriteFile("qa_data/tokens/ing_access_token.json")