Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkCertW.h>
#include <CkBinDataW.h>
#include <CkPrivateKeyW.h>
#include <CkHttpW.h>
#include <CkCrypt2W.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>
#include <CkRsaW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkCertW cert;
success = cert.LoadFromFile(L"qa_data/certs_and_keys/ING/example_client_tls.cer");
if (success == false) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
CkBinDataW bdPrivKey;
success = bdPrivKey.LoadFile(L"qa_data/certs_and_keys/ING/example_client_tls.key");
if (success == false) {
wprintf(L"Failed to load example_client_tls.key\n");
return;
}
// 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.
CkPrivateKeyW privKey;
success = privKey.LoadAnyFormat(bdPrivKey,L"");
if (success == false) {
wprintf(L"%s\n",privKey.lastErrorText());
return;
}
// Associate the private key with the certificate.
success = cert.SetPrivateKey(privKey);
if (success == false) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
CkHttpW http;
success = http.SetSslClientCert(cert);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
// 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
CkCrypt2W crypt;
crypt.put_HashAlgorithm(L"SHA256");
crypt.put_EncodingMode(L"base64");
const wchar_t *payload = L"grant_type=client_credentials";
const wchar_t *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")
CkDateTimeW dt;
dt.SetFromCurrentSystemTime();
// The desire date/time format is the "RFC822" format.
http.SetRequestHeader(L"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`
const wchar_t *httpMethod = L"POST";
const wchar_t *reqPath = L"/oauth2/token";
CkStringBuilderW sbStringToSign;
sbStringToSign.Append(L"(request-target): ");
sbStringToSign.Append(httpMethod);
sbStringToSign.ToLowercase();
sbStringToSign.Append(L" ");
sbStringToSign.AppendLine(reqPath,false);
sbStringToSign.Append(L"date: ");
sbStringToSign.AppendLine(dt.getAsRfc822(false),false);
sbStringToSign.Append(L"digest: SHA-256=");
sbStringToSign.Append(payloadDigest);
CkPrivateKeyW signingPrivKey;
success = signingPrivKey.LoadPemFile(L"qa_data/certs_and_keys/ING/example_client_signing.key");
if (success == false) {
wprintf(L"%s\n",signingPrivKey.lastErrorText());
return;
}
CkRsaW rsa;
success = rsa.UsePrivateKey(signingPrivKey);
if (success == false) {
wprintf(L"%s\n",rsa.lastErrorText());
return;
}
rsa.put_EncodingMode(L"base64");
const wchar_t *b64Signature = rsa.signStringENC(sbStringToSign.getAsString(),L"SHA256");
CkStringBuilderW sbAuthHdrVal;
sbAuthHdrVal.Append(L"Signature keyId=\"e77d776b-90af-4684-bebc-521e5b2614dd\",");
sbAuthHdrVal.Append(L"algorithm=\"rsa-sha256\",");
sbAuthHdrVal.Append(L"headers=\"(request-target) date digest\",");
sbAuthHdrVal.Append(L"signature=\"");
sbAuthHdrVal.Append(b64Signature);
sbAuthHdrVal.Append(L"\"");
CkStringBuilderW sbDigestHdrVal;
sbDigestHdrVal.Append(L"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"
CkHttpRequestW req;
req.AddParam(L"grant_type",L"client_credentials");
req.AddHeader(L"Accept",L"application/json");
req.AddHeader(L"Date",dt.getAsRfc822(false));
req.AddHeader(L"Digest",sbDigestHdrVal.getAsString());
req.AddHeader(L"Authorization",sbAuthHdrVal.getAsString());
req.put_HttpVerb(L"POST");
req.put_ContentType(L"application/x-www-form-urlencoded");
CkHttpResponseW resp;
success = http.HttpReq(L"https://api.sandbox.ing.com/oauth2/token",req,resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
// If successful, the status code = 200
wprintf(L"Response Status Code: %d\n",resp.get_StatusCode());
wprintf(L"%s\n",resp.bodyStr());
CkJsonObjectW json;
json.Load(resp.bodyStr());
json.put_EmitCompact(false);
wprintf(L"%s\n",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
const wchar_t *kty = 0;
const wchar_t *n = 0;
const wchar_t *e = 0;
const wchar_t *use = 0;
const wchar_t *alg = 0;
const wchar_t *x5t = 0;
const wchar_t *access_token = json.stringOf(L"access_token");
int expires_in = json.IntOf(L"expires_in");
const wchar_t *scope = json.stringOf(L"scope");
const wchar_t *token_type = json.stringOf(L"token_type");
const wchar_t *client_id = json.stringOf(L"client_id");
int i = 0;
int count_i = json.SizeOfArray(L"keys");
while (i < count_i) {
json.put_I(i);
kty = json.stringOf(L"keys[i].kty");
n = json.stringOf(L"keys[i].n");
e = json.stringOf(L"keys[i].e");
use = json.stringOf(L"keys[i].use");
alg = json.stringOf(L"keys[i].alg");
x5t = json.stringOf(L"keys[i].x5t");
i = i + 1;
}
// 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(L"qa_data/tokens/ing_access_token.json");
}