Lazarus Pascal Requires Chilkat v11.5.0+
Lazarus Pascal
Box: Authorize a User with OAuth 2.0
See more Box Examples
Interactive local-development example using a browser and a loopback callback. Register exactly http://localhost:3017/ in the Box User app. Run this in a trusted environment that can hold the client secret. A deployed web app must handle its registered HTTPS callback on the server.
See the Box API guide for setup and the complete workflow.
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.OAuth2,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
oauth2: TOAuth2;
url: string;
elapsedMs: Integer;
accessToken: string;
refreshToken: string;
tokens: TJsonObject;
begin
success := False;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success := False;
oauth2 := TOAuth2.Create;
oauth2.ListenPort := 3017;
oauth2.AuthorizationEndpoint := 'https://account.box.com/api/oauth2/authorize';
oauth2.TokenEndpoint := 'https://api.box.com/oauth2/token';
oauth2.ClientId := 'BOX_CLIENT_ID';
oauth2.ClientSecret := 'BOX_CLIENT_SECRET';
// Use Box's documented confidential-client authorization-code flow.
oauth2.CodeChallenge := False;
url := oauth2.StartAuth();
if (oauth2.LastMethodSuccess <> True) then
begin
WriteLn(oauth2.LastErrorText);
Exit;
end;
success := oauth2.LaunchBrowser(url);
if (success <> True) then
begin
WriteLn(oauth2.LastErrorText);
Exit;
end;
elapsedMs := 0;
while (elapsedMs < 120000) and (oauth2.AuthFlowState < 3) do
begin
oauth2.SleepMs(100);
elapsedMs := elapsedMs + 100;
end;
if (oauth2.AuthFlowState < 3) then
begin
oauth2.Cancel();
WriteLn('Authorization timed out.');
Exit;
end;
if (oauth2.AuthFlowState <> 3) then
begin
WriteLn('Authorization was denied or failed. State: ' + oauth2.AuthFlowState);
Exit;
end;
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
accessToken := oauth2.AccessToken;
refreshToken := oauth2.RefreshToken;
// For this local-development example only, save in a directory restricted to this user.
// Create qa_data/tokens before running; never put this file under a web root.
tokens := TJsonObject.Create;
success := tokens.Load(oauth2.AccessTokenResponse);
if (success <> True) then
begin
WriteLn(tokens.LastErrorText);
Exit;
end;
success := tokens.WriteFile('qa_data/tokens/box_oauth.json');
if (success <> True) then
begin
WriteLn(tokens.LastErrorText);
Exit;
end;
WriteLn('Authorization complete; token response saved in the protected local token file.');
oauth2.Free;
tokens.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.