Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

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

Dart
import 'package:chilkat/chilkat.dart';

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

  final cert = CkCert();
  try {
    cert.loadFromFile('qa_data/certs_and_keys/ING/example_client_tls.cer');
  } on ChilkatException catch (ex) {
    print(ex.lastErrorText);
    return;
  }

  final bdPrivKey = CkBinData();
  try {
    bdPrivKey.loadFile('qa_data/certs_and_keys/ING/example_client_tls.key');
  } on ChilkatException {
    print('Failed to load example_client_tls.key');
    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.

  final privKey = CkPrivateKey();
  try {
    privKey.loadAnyFormat(bdPrivKey, '');
  } on ChilkatException catch (ex) {
    print(ex.lastErrorText);
    return;
  }

  // Associate the private key with the certificate.
  try {
    cert.setPrivateKey(privKey);
  } on ChilkatException catch (ex) {
    print(ex.lastErrorText);
    return;
  }

  final http = CkHttp();

  try {
    http.setSslClientCert(cert);
  } on ChilkatException catch (ex) {
    print(ex.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
  final crypt = CkCrypt2();
  crypt.hashAlgorithm = 'SHA256';
  crypt.encodingMode = 'base64';
  final payload = 'grant_type=client_credentials';
  final 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")  
  final dt = CkDateTime();
  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`

  final httpMethod = 'POST';
  final reqPath = '/oauth2/token';

  final sbStringToSign = CkStringBuilder();
  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);

  final signingPrivKey = CkPrivateKey();
  try {
    signingPrivKey.loadPemFile('qa_data/certs_and_keys/ING/example_client_signing.key');
  } on ChilkatException catch (ex) {
    print(ex.lastErrorText);
    return;
  }

  final rsa = CkRsa();
  try {
    rsa.usePrivateKey(signingPrivKey);
  } on ChilkatException catch (ex) {
    print(ex.lastErrorText);
    return;
  }

  rsa.encodingMode = 'base64';
  final b64Signature = rsa.signStringENC(sbStringToSign.getAsString(), 'SHA256');

  final sbAuthHdrVal = CkStringBuilder();
  sbAuthHdrVal.append('Signature keyId="e77d776b-90af-4684-bebc-521e5b2614dd",');
  sbAuthHdrVal.append('algorithm="rsa-sha256",');
  sbAuthHdrVal.append('headers="(request-target) date digest",');
  sbAuthHdrVal.append('signature="');
  sbAuthHdrVal.append(b64Signature);
  sbAuthHdrVal.append('"');

  final sbDigestHdrVal = CkStringBuilder();
  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"

  final req = CkHttpRequest();
  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';

  final resp = CkHttpResponse();
  try {
    http.httpReq('https://api.sandbox.ing.com/oauth2/token', req, resp);
  } on ChilkatException catch (ex) {
    print(ex.lastErrorText);
    return;
  }

  // If successful, the status code = 200
  print('Response Status Code: ${resp.statusCode}');
  print(resp.bodyStr);

  final json = CkJsonObject();
  json.load(resp.bodyStr);

  json.emitCompact = false;
  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

  var kty = '';
  var n = '';
  var e = '';
  var use = '';
  var alg = '';
  var x5t = '';

  final accessToken = json.stringOf('access_token');
  final expiresIn = json.intOf('expires_in');
  final scope = json.stringOf('scope');
  final tokenType = json.stringOf('token_type');
  final clientId = json.stringOf('client_id');
  var i = 0;
  final countI = json.sizeOfArray('keys');
  while (i < countI) {
    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++;
  }

  // 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');
}