Authentication and connecting
TTMSFNCCloudGoogleCalendar reads and writes the calendars and events of a
Google account through the Google Calendar API, so each session begins with OAuth
2.0 authentication. You register an OAuth client in the Google Cloud console (with
the Calendar API enabled), give the component its client ID and secret, and it
runs the consent flow. After approval, the component holds the tokens that
authorize every calendar and event request. This chapter covers the credentials,
token persistence, and confirming the connection.
OAuth credentials
| 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. |
Authentication.CallBackPort |
The port the component listens on for the redirect; match it to the callback URL. |
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, then call
LoadTokens before connecting.
Connecting
Wire OnConnected before calling Connect. Connect is asynchronous — it
returns immediately and raises OnConnected once authentication completes; issue
your first request there.
procedure TForm1.SetupCalendar;
begin
FCalendar := TTMSFNCCloudGoogleCalendar.Create(Self);
// OAuth 2.0 client credentials from the Google Cloud console (Calendar API enabled).
FCalendar.Authentication.ClientID := '<your-client-id>.apps.googleusercontent.com';
FCalendar.Authentication.Secret := '<your-client-secret>';
FCalendar.Authentication.CallBackURL := 'http://127.0.0.1:8000';
FCalendar.Authentication.CallBackPort := 8000;
// Persist tokens so the consent screen only appears on first run.
FCalendar.PersistTokens.Location := plIniFile;
FCalendar.PersistTokens.Key := TTMSFNCUtils.AddBackslash(TTMSFNCUtils.GetDocumentsPath)
+ FCalendar.ClassName + '.ini';
FCalendar.LoadTokens;
FCalendar.OnConnected := CalendarConnected;
FCalendar.Connect; // opens the browser for consent on first run, then fires OnConnected
end;
procedure TForm1.CalendarConnected(Sender: TObject);
begin
// Tokens are valid here - safe to query calendars and events.
FCalendar.GetCalendars;
end;
Pitfalls
- Query only after
OnConnected.Connectreturns before authentication finishes. - Match
CallBackURLandCallBackPort, and register the redirect URI with the OAuth client exactly. - Enable the Google Calendar API for the OAuth client, or every request is rejected.
See also
- Managing calendars — list, create, and delete calendars.
- Managing events — read and create events.
- API reference — full class reference.