Google Calendar API
Google Calendar v3 API client — events, calendars, ACLs and notifications with OAuth 2.0 from Delphi.
Google Calendar v3 API client — events, calendars, ACLs and notifications with OAuth 2.0 from Delphi.
The Google Calendar API lets you integrate your app with Google Calendar, creating new ways for you to engage your users. The Calendar API lets you display, create and modify calendar events as well as work with many other calendar-related objects, such as calendars or access controls.
TsgcHTTPGoogleCloud_Calendar_Client| Standards & specs | Google Calendar API v3 reference |
| Component class | TsgcHTTPGoogleCloud_Calendar_Client (unit sgcHTTP_GoogleCloud_Calendar_Client) |
| Frameworks | VCL, FireMonkey, Lazarus / FPC |
| Platforms | Windows, macOS, Linux, iOS, Android |
The principal published / public properties used to configure and drive the component. Consult the online help for the full list.
TLSOptions | Published or public property used to configure or query the component. |
OnAuthToken | Published or public property used to configure or query the component. |
OnAuthTokenError | Published or public property used to configure or query the component. |
GoogleCloudOptions | Published or public property used to configure or query the component. |
LogFile | Published or public property used to configure or query the component. |
Scopes | Published or public property used to configure or query the component. |
OnGetCalendar | Published or public property used to configure or query the component. |
OnGetCalendarEvent | Published or public property used to configure or query the component. |
OnError | Published or public property used to configure or query the component. |
Calendars | Published or public property used to configure or query the component. |
The principal public methods exposed by the component.
ACL_Get() | Public function exposed by the component. |
CalendarList_Delete() | Public function exposed by the component. |
CalendarList_Get() | Public function exposed by the component. |
CalendarList_Patch() | Public function exposed by the component. |
Calendar_Get() | Public function exposed by the component. |
Color_Get() | Public function exposed by the component. |
Event_Get() | Public function exposed by the component. |
DeleteCalendar() | Public function exposed by the component. |
DeleteEvent() | Public function exposed by the component. |
ACL_Delete() | Public function exposed by the component. |
Drop the component on a form, configure the properties below and activate it. The snippet that follows shows the typical Google Calendar | Load Calendars configuration sourced from the online help.
oGoogleCalendar := TsgcHTTPGoogleCloud_Calendar_Client.Create(nil); // ... configure OAuth2 options oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientId := 'google ClientId'; oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientSecret := 'google ClientSecret'; // ... request calendars if oGoogleCalendar.LoadCalendars then begin // ... get calendars data for i := 0 to oGoogleCalendar.Calendars.Count - 1 do vCalendarTitle := oGoogleCalendar.Calendars.Calendar[i].Summary; end else raise Exception.Create('Error Calendar Sync');
TsgcHTTPGoogleCloud_Calendar_Client oGoogleCalendar = new TsgcHTTPGoogleCloud_Calendar_Client(NULL); // ... configure OAuth2 options oGoogleCalendar->GoogleCloudOptions->OAuth2->ClientId = "google ClientId"; oGoogleCalendar->GoogleCloudOptions->OAuth2->ClientSecret = "google ClientSecret"; // ... request calendars if (oGoogleCalendar->LoadCalendars) { // ... get calendars data for (int i = 0; i < oGoogleCalendar->Calendars->Count; i++) { vCalendarTitle = oGoogleCalendar->Calendars->Calendar[i]->Summary; } } else { throw Exception("Error Calendar Sync"); }
TsgcHTTPGoogleCloud_Calendar_Client oGoogleCalendar = new TsgcHTTPGoogleCloud_Calendar_Client(); // ... configure OAuth2 options oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientId = "google ClientId"; oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientSecret = "google ClientSecret"; // ... request calendars if (oGoogleCalendar.LoadCalendars()) { // ... get calendars data for (int i = 0; i < oGoogleCalendar.Calendars.Count; i++) { string vCalendarTitle = oGoogleCalendar.Calendars.Calendar[i].Summary; } } else { throw new Exception("Error Calendar Sync"); }
The following scenarios are lifted verbatim from the online help. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
The process to get all events from a calendar is very easy. Just follow the next steps:
oGoogleCalendar := TsgcHTTPGoogleCloud_Calendar_Client.Create(nil); // ... configure OAuth2 options oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientId := 'google ClientId'; oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientSecret := 'google ClientSecret'; // ... request calendars first oGoogleCalendar.LoadCalendars; // ... request events from first calendar oCalendar := TsgcGoogleCalendarItem(oGoogleCalendar.Calendars.Calendar[0]); if oGoogleCalendar.LoadEvents(oCalendar.ID) then begin // ... get events data for i := 0 to oCalendar.Events.Count - 1 do vEventTitle := oCalendar.Events[i].Summary; end else raise Exception.Create('Error Event Sync');
TsgcHTTPGoogleCloud_Calendar_Client oGoogleCalendar = new TsgcHTTPGoogleCloud_Calendar_Client(NULL); // ... configure OAuth2 options oGoogleCalendar->GoogleCloudOptions->OAuth2->ClientId = "google ClientId"; oGoogleCalendar->GoogleCloudOptions->OAuth2->ClientSecret = "google ClientSecret"; // ... request calendars first; oGoogleCalendar->LoadCalendars; // ... request events from first calendar oCalendar = TsgcGoogleCalendarItem(oGoogleCalendar->Calendars->Calendar[0]); if (oGoogleCalendar->LoadEvents(oCalendar->ID)) { // ... get events data for (int i = 0; i < oCalendar->Events->Count; i++) { vEventTitle = oCalendar->Events[i]->Summary; } } else { throw Exception("Error Event Sync"); }
TsgcHTTPGoogleCloud_Calendar_Client oGoogleCalendar = new TsgcHTTPGoogleCloud_Calendar_Client(); // ... configure OAuth2 options oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientId = "google ClientId"; oGoogleCalendar.GoogleCloudOptions.OAuth2.ClientSecret = "google ClientSecret"; // ... request calendars first oGoogleCalendar.LoadCalendars(); // ... request events from first calendar TsgcGoogleCalendarItem oCalendar = (TsgcGoogleCalendarItem)oGoogleCalendar.Calendars.Calendar[0]; if (oGoogleCalendar.LoadEvents(oCalendar.ID)) { // ... get events data for (int i = 0; i < oCalendar.Events.Count; i++) { string vEventTitle = oCalendar.Events[i].Summary; } } else { throw new Exception("Error Event Sync"); }
Google Calendar component client has the following properties:
oCalendar := TsgcHTTPGoogleCloud_Calendar_Client.Create(nil); oCalendar.TLSOptions.IOHandler := iohOpenSSL; oCalendar.TLSOptions.Version := tls1_3; oCalendar.TLSOptions.VerifyCertificate := True; oCalendar.TLSOptions.OpenSSL_Options.LibPath := oslpDefaultFolder;
TsgcHTTPGoogleCloud_Calendar_Client *oCalendar = new TsgcHTTPGoogleCloud_Calendar_Client(NULL); oCalendar->TLSOptions->IOHandler = iohOpenSSL; oCalendar->TLSOptions->Version = tls1_3; oCalendar->TLSOptions->VerifyCertificate = true; oCalendar->TLSOptions->OpenSSL_Options->LibPath = oslpDefaultFolder;
oCalendar = new TsgcHTTPGoogleCloud_Calendar_Client(); oCalendar.TLSOptions.IOHandler = TwsTLSIOHandler.iohOpenSSL; oCalendar.TLSOptions.Version = TwsTLSVersions.tls1_3; oCalendar.TLSOptions.VerifyCertificate = true; oCalendar.TLSOptions.OpenSSL_Options.LibPath = oslpDefaultFolder;
The first time you authenticate, use the OnAuthToken event to save the RefreshToken if it exists. You can save it in an INI file, for example:
procedure OnGoogleCalendarAuthToken(Sender: TObject; const TokenType, Token, Data: string); var oINI: TINIFile; oJSON: TsgcJSON; begin oJSON := TsgcJSON.Create(nil); Try oJSON.Read(Data); if oJSON.Node['refresh_token'] nil then begin oINI := TINIFile.Create(ChangeFileExt(Application.ExeName, '.ini')); Try oINI.WriteString('OAUTH2', 'Token', oJSON.Node['refresh_token'].Value); Finally oINI.Free; End; end; Finally oJSON.Free; End; end;
void OnGoogleCalendarAuthToken(TObject *Sender, string TokenType, string Token, string Data) { TsgcJSON *oJSON = new TsgcJSON(); try { oJSON->Read(Data); if (oJSON->Node["refresh_token"] != NULL) { TINIFile *oINI = new TINIFile(ChangeFileExt(Application->ExeName, ".ini")); try { oINI->WriteString("OAUTH2", "Token", oJSON->Node["refresh_token"]->Value); } __finally { oINI->Free(); } } } __finally { oJSON->Free(); } }
void OnGoogleCalendarAuthToken(object sender, string TokenType, string Token, string Data) { TsgcJSON oJSON = new TsgcJSON(); oJSON.Read(Data); if (oJSON.Node["refresh_token"] != null) { // Save refresh token to your preferred storage string refreshToken = oJSON.Node["refresh_token"].Value; System.IO.File.WriteAllText("refresh_token.txt", refreshToken); } }
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Demos\20.HTTP_Protocol\03.Google\02.Google_Calendar