Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

ABN AMRO OAuth2 Client Credentials Authentication

See more ABN AMRO Examples

Demonstrates how to obtain an access token for an ABN AMRO online API using OAuth2 with the Client Credentials flow.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.HttpResponse,
  Chilkat.JsonObject,
  Chilkat.HttpRequest,
  Chilkat.BinData,
  Chilkat.PrivateKey,
  Chilkat.Cert,
  Chilkat.Http;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  bdKey: TBinData;
  privKey: TPrivateKey;
  http: THttp;
  req: THttpRequest;
  resp: THttpResponse;
  json: TJsonObject;

begin
  success := False;

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

  //  This example sends the following CURL request:

  //  	curl -X POST -k https://auth-sandbox.connect.abnamro.com:8443/as/token.oauth2 \
  //  	-v \
  //  	--cert TPPCertificate.crt \
  //  	--key TPPprivateKey.key \
  //  	-H 'Cache-Control: no-cache' \
  //  	-H 'Content-Type: application/x-www-form-urlencoded' \
  //  	-d 'grant_type=client_credentials&client_id=TPP_test&scope=psd2:payment:sepa:write psd2:payment:sepa:read'

  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/certs/TPPCertificate.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  bdKey := TBinData.Create;
  success := bdKey.LoadFile('qa_data/certs/TPPprivateKey.key');

  privKey := TPrivateKey.Create;
  success := privKey.LoadAnyFormat(bdKey,'passwordIfNeeded');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  success := cert.SetPrivateKey(privKey);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  http := THttp.Create;

  success := http.SetSslClientCert(cert);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  req := THttpRequest.Create;
  req.AddParam('grant_type','client_credentials');
  req.AddParam('client_id','TPP_test');
  req.AddParam('scope','psd2:payment:sepa:write psd2:payment:sepa:read');

  req.HttpVerb := 'POST';
  req.ContentType := 'application/x-www-form-urlencoded';

  resp := THttpResponse.Create;
  success := http.HttpReq('https://auth-sandbox.connect.abnamro.com:8443/as/token.oauth2',req,resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  if (resp.StatusCode <> 200) then
    begin
      WriteLn(resp.BodyStr);
      Exit;
    end;

  //  Get the JSON result:
  //  {"access_token":"TIhycwl8rfrZPkXGw15mwldASAAK","token_type":"Bearer","expires_in":7200}
  json := TJsonObject.Create;
  json.Load(resp.BodyStr);
  WriteLn('access_token: ' + json.StringOf('access_token'));
  WriteLn('token_type: ' + json.StringOf('token_type'));
  WriteLn('expires_in: ' + json.StringOf('expires_in'));


  cert.Free;
  bdKey.Free;
  privKey.Free;
  http.Free;
  req.Free;
  resp.Free;
  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.