Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
RSAP Union API - Get OAuth2 Access Token
See more _Miscellaneous_ Examples
Demonstrates how to get an OAuth2 access token for the RSAP Union API. Note: This uses the client credentials flow, which does NOT require an interactive engagement using a browser.Chilkat Pascal (Lazarus/Delphi) Downloads
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.StringBuilder,
Chilkat.JsonObject,
Chilkat.PrivateKey,
Chilkat.Cert,
Chilkat.Http;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
json: TJsonObject;
cert: TCert;
privKey: TPrivateKey;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// The following JSON is sent in the request body.
// {
// "grant_type": "client_credentials",
// "client_id": 1234,
// "client_secret": "23456abcde"
// }
json := TJsonObject.Create;
json.UpdateString('grant_type','client_credentials');
json.UpdateInt('client_id',1234);
json.UpdateString('client_secret','23456abcde');
http.SetRequestHeader('Content-type','application/json');
// Add the client certificate TLS authentication.
cert := TCert.Create;
success := cert.LoadFromFile('qa_data/certs_and_keys/union_client_certificate.crt');
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
privKey := TPrivateKey.Create;
success := privKey.LoadAnyFormatFile('qa_data/certs_and_keys/union_client_certificate.nopass.key','');
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
// Associate the private key with the cert.
// This will fail if the private key is not actually the correct one that corresponds to the public key stored within the cert.
success := cert.SetPrivateKey(privKey);
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Tell HTTP to use the cert for client TLS certificate authentication.
success := http.SetSslClientCert(cert);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://api-test.rsap.ca/oauth/token',json,'application/json',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;
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "token_type": "Bearer",
// "expires_in": 3600,
// "access_token": "eyJ0eXAi...LnE"
// }
// This token expires in 1 hour. Your application could re-use the same token for up to an hour,
// or it can simply get a new access token before each request (if you're not doing too many requests).
success := jResp.WriteFile('qa_data/tokens/rsapToken.json');
http.Free;
json.Free;
cert.Free;
privKey.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.