Table of Contents

Authentication and connecting

TTMSFNCCloudGoogleFireBaseObjectDatabase stores and retrieves Delphi objects in a Google Firebase Realtime Database. Like the other Cloud Pack components it authenticates with OAuth 2.0: you register an OAuth client in the Google Cloud console, give the component its client ID and secret, and it runs the consent flow. In addition to the credentials you point the component at a specific database and table (node) before connecting. This chapter covers the credentials, the database/table target, token persistence, and confirming the connection.

OAuth credentials and target

Property Purpose
Authentication.ClientID The OAuth client ID from the Google Cloud console.
Authentication.Secret The matching client secret.
Authentication.CallBackURL The local redirect URI that receives the authorization code, e.g. http://127.0.0.1:8000.
DatabaseName The Firebase Realtime Database to connect to.
TableName The node under the database that objects are stored in.

Set Logging := True while developing to trace the underlying requests.

Token reuse across runs

Calling Connect the first time opens the system browser for the consent screen. To avoid repeating that, persist the tokens: set PersistTokens.Location to plIniFile and PersistTokens.Key to a writable file path, call LoadTokens before connecting, and SaveTokens once connected.

Connecting

Wire OnConnected before calling Connect. Connect is asynchronous — it returns immediately and raises OnConnected when authentication completes; that is the moment to save tokens and issue the first read.

procedure TForm1.SetupFireBase;
begin
  FDatabase := TTMSFNCCloudGoogleFireBaseObjectDatabase.Create(Self);

  // OAuth 2.0 client credentials from the Google Cloud console.
  FDatabase.Authentication.ClientID := '<your-client-id>.apps.googleusercontent.com';
  FDatabase.Authentication.Secret := '<your-client-secret>';
  FDatabase.Authentication.CallBackURL := 'http://127.0.0.1:8000';

  // Target Realtime Database and the node (table) objects are stored under.
  FDatabase.DatabaseName := '<your-firebase-database>';
  FDatabase.TableName := 'Contacts';

  // Persist tokens so the consent screen only appears on first run.
  FDatabase.PersistTokens.Location := plIniFile;
  FDatabase.PersistTokens.Key := TTMSFNCUtils.AddBackslash(TTMSFNCUtils.GetDocumentsPath)
    + FDatabase.ClassName + '.ini';
  FDatabase.LoadTokens;

  FDatabase.OnConnected := DatabaseConnected;
  FDatabase.Connect; // opens the browser for consent on first run, then fires OnConnected
end;

procedure TForm1.DatabaseConnected(Sender: TObject);
begin
  FDatabase.SaveTokens;
  // Safe to read or write objects now.
  FDatabase.ReadList(FObjects);
end;

Pitfalls

  • Read or write only after OnConnected. Connect returns before authentication finishes.
  • DatabaseName and TableName must be set before connecting — they decide where objects are read from and written to.
  • The callback URL must match the registered redirect URI exactly, including scheme, host, and port.

See also