Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.5.0+

Box: Identify the User Behind an Access Token

See more Box Examples

Identify the account whose permissions and root folder the token uses. Start here when an app cannot see files visible in a personal Box account.

See the Box API guide for setup and the complete workflow.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.HttpCurl;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  curl: THttpCurl;
  authorizedCommand1: TStringBuilder;
  headerReplacement1: Integer;

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;

  //  Supply an unexpired token obtained with one of the Box authentication examples.
  //  A token represents a particular user or service account. IDs must be accessible to it.
  curl := THttpCurl.Create;
  curl.SetVar('access_token','BOX_ACCESS_TOKEN');

  curl.ClearTargetOutput('*');
  authorizedCommand1 := TStringBuilder.Create;
  authorizedCommand1.SetString('curl -X GET "https://api.box.com/2.0/users/me?fields=id,name,login" -H "Authorization: Bearer {{access_token}}"');
  headerReplacement1 := authorizedCommand1.Replace('{{access_token}}',curl.GetVar('access_token'));
  success := curl.DoYourThing(authorizedCommand1.GetAsString());
  if ((success <> True) or (curl.StatusCode <> 200)) then
    begin
      WriteLn('Box request failed. HTTP status: ' + curl.StatusCode);
      WriteLn('Failure reason: ' + curl.FailReason);
      WriteLn(curl.ResponseBodyStr);
      Exit;
    end;
  WriteLn(curl.ResponseBodyStr);


  curl.Free;
  authorizedCommand1.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.