Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
ChartURL - Create a Signed URL
See more HTTP Misc Examples
Demonstrates how to create a signed URL for ChartURL.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.StringBuilder,
Chilkat.JsonObject,
Chilkat.Crypt2;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
crypt: TCrypt2;
key: string;
token: string;
slug: string;
data: string;
json: TJsonObject;
sig: string;
sbUrl: TStringBuilder;
begin
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
crypt := TCrypt2.Create;
// Example key: "dek-d7a46236eda961a6c3c18ffcc6b077ba87d27e9ae85f7842c6d427c265dd5f69d5131308d93332353d4a55a4b1160fcf516515a4a9f0aa50fbf2d7a2e7d0f1c5"
key := 'charturl-sign-encrypt-key';
// Example token: "dt-RwYN"
token := 'charturl-token';
slug := 'weekly-activity';
data := '{ "options": {"data": {"columns": [["This Week",10,13],["Last Week",9,5]]}}}';
crypt.HashAlgorithm := 'SHA256';
crypt.MacAlgorithm := 'HMAC';
crypt.SetMacKeyString(key);
crypt.EncodingMode := 'base64';
json := TJsonObject.Create;
json.Load(data);
WriteLn('json = ' + json.Emit());
sig := crypt.MacStringENC(json.Emit());
sbUrl := TStringBuilder.Create;
sbUrl.Append('https://charturl.com/i/');
sbUrl.Append(token);
sbUrl.Append('/');
sbUrl.Append(slug);
sbUrl.Append('?d=');
sbUrl.Append(crypt.EncodeString(json.Emit(),'utf-8','url'));
sbUrl.Append('&s=');
sbUrl.Append(crypt.EncodeString(sig,'utf-8','url'));
WriteLn('Signed URL: ' + sbUrl.GetAsString());
crypt.Free;
json.Free;
sbUrl.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.