Sample code for 30+ languages & platforms
Lazarus Pascal

Explicitly set the OAuth2 Access Token

This example shows how to set the AuthToken property using a previously obtained access token.

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.JsonObject,
  Chilkat.Http;

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

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;
  http: THttp;

begin
  success := False;

  //  Assume we previously obtained JSON containing the access token such as this:

  //  	{
  //  	 "access_token": "ya39.Ci-XA_C5bGgRDC3UaD-h0_NeL-DVIQnI2gHtBBBHkZzrwlARkwX6R3O0PCDEzRlfaQ",
  //  	 "token_type": "Bearer",
  //  	 "expires_in": 3600,
  //  	 "refresh_token": "1/r_2c_7jddspcdfesrrfKqfXtqo08D6Q-gUU0DsdfVMsx0c"
  //  	}
  //  

  //  Load the JSON and use the access_token to authenticate an HTTP request.
  json := TJsonObject.Create;
  success := json.LoadFile('c:/someDir/tokens/myToken.json');

  http := THttp.Create;

  //  After setting this property, all HTTP requests sent using this object instance
  //  will include the request header: Authorization: Bearer <access_token> 
  http.AuthToken := json.StringOf('access_token');

  //  ...
  //  ...


  json.Free;
  http.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.