Delphi DLL Requires Chilkat v11.5.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
oauth2: HCkOAuth2;
url: PWideChar;
elapsedMs: Integer;
accessToken: PWideChar;
refreshToken: PWideChar;
tokens: HCkJsonObject;
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 := CkOAuth2_Create();
CkOAuth2_putListenPort(oauth2,3017);
CkOAuth2_putAuthorizationEndpoint(oauth2,'https://account.box.com/api/oauth2/authorize');
CkOAuth2_putTokenEndpoint(oauth2,'https://api.box.com/oauth2/token');
CkOAuth2_putClientId(oauth2,'BOX_CLIENT_ID');
CkOAuth2_putClientSecret(oauth2,'BOX_CLIENT_SECRET');
// Use Box's documented confidential-client authorization-code flow.
CkOAuth2_putCodeChallenge(oauth2,False);
url := CkOAuth2__startAuth(oauth2);
if (CkOAuth2_getLastMethodSuccess(oauth2) <> True) then
begin
Memo1.Lines.Add(CkOAuth2__lastErrorText(oauth2));
Exit;
end;
success := CkOAuth2_LaunchBrowser(oauth2,url);
if (success <> True) then
begin
Memo1.Lines.Add(CkOAuth2__lastErrorText(oauth2));
Exit;
end;
elapsedMs := 0;
while (elapsedMs < 120000) and (CkOAuth2_getAuthFlowState(oauth2) < 3) do
begin
CkOAuth2_SleepMs(oauth2,100);
elapsedMs := elapsedMs + 100;
end;
if (CkOAuth2_getAuthFlowState(oauth2) < 3) then
begin
CkOAuth2_Cancel(oauth2);
Memo1.Lines.Add('Authorization timed out.');
Exit;
end;
if (CkOAuth2_getAuthFlowState(oauth2) <> 3) then
begin
Memo1.Lines.Add('Authorization was denied or failed. State: ' + IntToStr(CkOAuth2_getAuthFlowState(oauth2)));
Exit;
end;
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
accessToken := CkOAuth2__accessToken(oauth2);
refreshToken := CkOAuth2__refreshToken(oauth2);
// 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 := CkJsonObject_Create();
success := CkJsonObject_Load(tokens,CkOAuth2__accessTokenResponse(oauth2));
if (success <> True) then
begin
Memo1.Lines.Add(CkJsonObject__lastErrorText(tokens));
Exit;
end;
success := CkJsonObject_WriteFile(tokens,'qa_data/tokens/box_oauth.json');
if (success <> True) then
begin
Memo1.Lines.Add(CkJsonObject__lastErrorText(tokens));
Exit;
end;
Memo1.Lines.Add('Authorization complete; token response saved in the protected local token file.');
CkOAuth2_Dispose(oauth2);
CkJsonObject_Dispose(tokens);