Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Cert
oleobject loo_BdPrivKey
oleobject loo_PrivKey
oleobject loo_Http
oleobject loo_Crypt
string ls_Payload
string ls_PayloadDigest
oleobject loo_Dt
string ls_HttpMethod
string ls_ReqPath
oleobject loo_SbStringToSign
oleobject loo_SigningPrivKey
oleobject loo_Rsa
string ls_B64Signature
oleobject loo_SbAuthHdrVal
oleobject loo_SbDigestHdrVal
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Json
string ls_Kty
string n
string e
string ls_Use
string ls_Alg
string ls_X5t
string ls_Access_token
integer li_Expires_in
string ls_Scope
string ls_Token_type
string ls_Client_id
integer i
integer li_Count_i

li_Success = 0

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

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
if li_rc < 0 then
    destroy loo_Cert
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Cert.LoadFromFile("qa_data/certs_and_keys/ING/example_client_tls.cer")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Cert
    return
end if

loo_BdPrivKey = create oleobject
li_rc = loo_BdPrivKey.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdPrivKey.LoadFile("qa_data/certs_and_keys/ING/example_client_tls.key")
if li_Success = 0 then
    Write-Debug "Failed to load example_client_tls.key"
    destroy loo_Cert
    destroy loo_BdPrivKey
    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.

loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadAnyFormat(loo_BdPrivKey,"")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Cert
    destroy loo_BdPrivKey
    destroy loo_PrivKey
    return
end if

// Associate the private key with the certificate.
li_Success = loo_Cert.SetPrivateKey(loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Cert
    destroy loo_BdPrivKey
    destroy loo_PrivKey
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

li_Success = loo_Http.SetSslClientCert(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Cert
    destroy loo_BdPrivKey
    destroy loo_PrivKey
    destroy loo_Http
    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
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

loo_Crypt.HashAlgorithm = "SHA256"
loo_Crypt.EncodingMode = "base64"
ls_Payload = "grant_type=client_credentials"
ls_PayloadDigest = loo_Crypt.HashStringENC(ls_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")  
loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime")

loo_Dt.SetFromCurrentSystemTime()
// The desire date/time format is the "RFC822" format.
loo_Http.SetRequestHeader("Date",loo_Dt.GetAsRfc822(0))

// 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`

ls_HttpMethod = "POST"
ls_ReqPath = "/oauth2/token"

loo_SbStringToSign = create oleobject
li_rc = loo_SbStringToSign.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbStringToSign.Append("(request-target): ")
loo_SbStringToSign.Append(ls_HttpMethod)
loo_SbStringToSign.ToLowercase()
loo_SbStringToSign.Append(" ")
loo_SbStringToSign.AppendLine(ls_ReqPath,0)

loo_SbStringToSign.Append("date: ")
loo_SbStringToSign.AppendLine(loo_Dt.GetAsRfc822(0),0)

loo_SbStringToSign.Append("digest: SHA-256=")
loo_SbStringToSign.Append(ls_PayloadDigest)

loo_SigningPrivKey = create oleobject
li_rc = loo_SigningPrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_SigningPrivKey.LoadPemFile("qa_data/certs_and_keys/ING/example_client_signing.key")
if li_Success = 0 then
    Write-Debug loo_SigningPrivKey.LastErrorText
    destroy loo_Cert
    destroy loo_BdPrivKey
    destroy loo_PrivKey
    destroy loo_Http
    destroy loo_Crypt
    destroy loo_Dt
    destroy loo_SbStringToSign
    destroy loo_SigningPrivKey
    return
end if

loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

li_Success = loo_Rsa.UsePrivateKey(loo_SigningPrivKey)
if li_Success = 0 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Cert
    destroy loo_BdPrivKey
    destroy loo_PrivKey
    destroy loo_Http
    destroy loo_Crypt
    destroy loo_Dt
    destroy loo_SbStringToSign
    destroy loo_SigningPrivKey
    destroy loo_Rsa
    return
end if

loo_Rsa.EncodingMode = "base64"
ls_B64Signature = loo_Rsa.SignStringENC(loo_SbStringToSign.GetAsString(),"SHA256")

loo_SbAuthHdrVal = create oleobject
li_rc = loo_SbAuthHdrVal.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbAuthHdrVal.Append("Signature keyId=~"e77d776b-90af-4684-bebc-521e5b2614dd~",")
loo_SbAuthHdrVal.Append("algorithm=~"rsa-sha256~",")
loo_SbAuthHdrVal.Append("headers=~"(request-target) date digest~",")
loo_SbAuthHdrVal.Append("signature=~"")
loo_SbAuthHdrVal.Append(ls_B64Signature)
loo_SbAuthHdrVal.Append("~"")

loo_SbDigestHdrVal = create oleobject
li_rc = loo_SbDigestHdrVal.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbDigestHdrVal.Append("SHA-256=")
loo_SbDigestHdrVal.Append(ls_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"

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.AddParam("grant_type","client_credentials")
loo_Req.AddHeader("Accept","application/json")
loo_Req.AddHeader("Date",loo_Dt.GetAsRfc822(0))
loo_Req.AddHeader("Digest",loo_SbDigestHdrVal.GetAsString())
loo_Req.AddHeader("Authorization",loo_SbAuthHdrVal.GetAsString())

loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpReq("https://api.sandbox.ing.com/oauth2/token",loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Cert
    destroy loo_BdPrivKey
    destroy loo_PrivKey
    destroy loo_Http
    destroy loo_Crypt
    destroy loo_Dt
    destroy loo_SbStringToSign
    destroy loo_SigningPrivKey
    destroy loo_Rsa
    destroy loo_SbAuthHdrVal
    destroy loo_SbDigestHdrVal
    destroy loo_Req
    destroy loo_Resp
    return
end if

// If successful, the status code = 200
Write-Debug "Response Status Code: " + string(loo_Resp.StatusCode)
Write-Debug loo_Resp.BodyStr

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.Load(loo_Resp.BodyStr)

loo_Json.EmitCompact = 0
Write-Debug loo_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

ls_Access_token = loo_Json.StringOf("access_token")
li_Expires_in = loo_Json.IntOf("expires_in")
ls_Scope = loo_Json.StringOf("scope")
ls_Token_type = loo_Json.StringOf("token_type")
ls_Client_id = loo_Json.StringOf("client_id")
i = 0
li_Count_i = loo_Json.SizeOfArray("keys")
do while i < li_Count_i
    loo_Json.I = i
    ls_Kty = loo_Json.StringOf("keys[i].kty")
    n = loo_Json.StringOf("keys[i].n")
    e = loo_Json.StringOf("keys[i].e")
    ls_Use = loo_Json.StringOf("keys[i].use")
    ls_Alg = loo_Json.StringOf("keys[i].alg")
    ls_X5t = loo_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.
loo_Json.WriteFile("qa_data/tokens/ing_access_token.json")


destroy loo_Cert
destroy loo_BdPrivKey
destroy loo_PrivKey
destroy loo_Http
destroy loo_Crypt
destroy loo_Dt
destroy loo_SbStringToSign
destroy loo_SigningPrivKey
destroy loo_Rsa
destroy loo_SbAuthHdrVal
destroy loo_SbDigestHdrVal
destroy loo_Req
destroy loo_Resp
destroy loo_Json