Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
PayPal: Get a sandbox access token
See more PayPal Examples
Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.
Chilkat Lazarus Pascal 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.Http,
Chilkat.HttpResponse,
Chilkat.CkDateTime,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
issued: TCkDateTime;
resp: THttpResponse;
errorInfo: TJsonObject;
result: TJsonObject;
parsed: Boolean;
accessToken: string;
lifetime: Integer;
timing: TJsonObject;
savedTiming: Boolean;
begin
success := False;
// See Unlock Chilkat before making requests.
http := THttp.Create;
// Load these values from the credential store on the merchant backoffice workstation.
// Never embed the merchant secret in an app distributed to customers.
http.Login := 'SANDBOX_CLIENT_ID';
http.Password := 'SANDBOX_CLIENT_SECRET';
http.BasicAuth := True;
issued := TCkDateTime.Create;
issued.SetFromCurrentSystemTime();
resp := THttpResponse.Create;
success := http.HttpStr('POST','https://api-m.sandbox.paypal.com/v1/oauth2/token','grant_type=client_credentials','utf-8','application/x-www-form-urlencoded',resp);
if (success <> True) then
begin
WriteLn('No HTTP response: ' + http.LastErrorText);
Exit;
end;
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
errorInfo := TJsonObject.Create;
errorInfo.Load(resp.BodyStr);
WriteLn('HTTP ' + resp.StatusCode + ' ' + errorInfo.StringOf('error') + ' ' + errorInfo.StringOf('error_description'));
WriteLn('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
Exit;
end;
result := TJsonObject.Create;
parsed := result.Load(resp.BodyStr);
if (parsed <> True) then
begin
WriteLn('Expected a JSON response; HTTP ' + resp.StatusCode);
Exit;
end;
accessToken := result.StringOf('access_token');
lifetime := result.IntOf('expires_in');
if ((result.HasMember('access_token') <> True) or (lifetime <= 0)) then
begin
WriteLn('Missing token or lifetime.');
Exit;
end;
// Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
// Save only timing metadata here, relative to the start of the token request.
timing := TJsonObject.Create;
timing.UpdateString('issued_at',issued.GetAsTimestamp(False));
timing.UpdateInt('expires_in',lifetime);
savedTiming := timing.WriteFile('paypal-token-timing.json');
if (savedTiming <> True) then
begin
WriteLn('Could not save token timing.');
Exit;
end;
WriteLn('Token received; lifetime in seconds: ' + lifetime);
http.Free;
issued.Free;
resp.Free;
errorInfo.Free;
result.Free;
timing.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.