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

citi Developer OAuth2 Client Credentials Grant

See more OAuth2 Examples

Get access token for your application credentials. You can use this for citi APIs which do not require customer credential verification and consent (e.g. Onboarding).

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.Http,
  Chilkat.HttpRequest,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  req: THttpRequest;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  token_type: string;
  access_token: string;
  expires_in: Integer;
  consented_on: Integer;
  scope: string;

begin
  success := False;

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

  http := THttp.Create;

  //  Implements the following CURL command:

  //  curl --request POST \
  //    --url https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb \
  //    --header 'accept: application/json' \
  //    --user client-id:client-secret \
  //    --header 'content-type: application/x-www-form-urlencoded' \
  //    --data 'grant_type=client_credentials&scope=%2Fapi'

  http.Login := 'client-id';
  http.Password := 'client-secret';

  req := THttpRequest.Create;
  req.HttpVerb := 'POST';
  req.Path := '/gcb/api/clientCredentials/oauth2/token/us/gcb';
  req.ContentType := 'application/x-www-form-urlencoded';
  req.AddParam('grant_type','client_credentials');
  req.AddParam('scope','/api');
  req.AddHeader('accept','application/json');

  resp := THttpResponse.Create;
  success := http.HttpReq('https://sandbox.apihub.citi.com/gcb/api/clientCredentials/oauth2/token/us/gcb',req,resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;

  success := jResp.WriteFile('qa_data/tokens/citi_client_credentials.json');
  if (success = False) then
    begin
      WriteLn('Failed to save JSON access token file.');
      Exit;
    end;

  //   Sample JSON response:
  //   (Sample code for parsing the JSON response is shown below)

  //   {
  //     "token_type": "bearer",
  //     "access_token": "AAIkMjdh ... 3fsWb7zJ0s",
  //     "expires_in": 1800,
  //     "consented_on": 1584817860,
  //     "scope": "/api"
  //   }

  //   Sample code for parsing the JSON response...
  //   Use the following online tool to generate parsing code from sample JSON:
  //   Generate Parsing Code from JSON

  token_type := jResp.StringOf('token_type');
  access_token := jResp.StringOf('access_token');
  expires_in := jResp.IntOf('expires_in');
  consented_on := jResp.IntOf('consented_on');
  scope := jResp.StringOf('scope');


  http.Free;
  req.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.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.