Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat Pascal (Lazarus/Delphi) 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.StringBuilder,
Chilkat.AuthGoogle,
Chilkat.Rest;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
bAutoReconnect: Boolean;
gAuth: TAuthGoogle;
sbJsonKey: TStringBuilder;
bLoaded: Boolean;
jsonKey: string;
responseText: string;
begin
success := False;
rest := TRest.Create;
bTls := True;
bAutoReconnect := True;
success := rest.Connect('example.com',443,bTls,bAutoReconnect);
ERROR: "=" expected
ERROR: Undefined variable(CHECK_SUCCESS)
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
gAuth := TAuthGoogle.Create;
sbJsonKey := TStringBuilder.Create;
bLoaded := sbJsonKey.LoadFile('qa_data/service_account.json');
if (bLoaded <> True) then
begin
WriteLn('Failed to load the service account JSON key.');
Exit;
end;
jsonKey := sbJsonKey.GetAsString();
if (sbJsonKey.LastMethodSuccess = False) then
begin
WriteLn(sbJsonKey.LastErrorText);
Exit;
end;
gAuth.JsonKey := jsonKey;
// A space-delimited list of the requested permissions.
gAuth.Scope := 'https://www.googleapis.com/auth/cloud-platform';
// Associate the provider so Chilkat supplies a Google bearer token on each request.
success := rest.SetAuthGoogle(gAuth);
ERROR: "=" expected
ERROR: Undefined variable(CHECK_SUCCESS)
responseText := rest.FullRequestNoBody('GET','/storage/v1/b?project=my-project');
ERROR: "=" expected
ERROR: Undefined variable(CHECK_LMS)
WriteLn(responseText);
rest.Free;
gAuth.Free;
sbJsonKey.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.