Delphi DLL Google Calendar: List Calendars

Back to Index

Returns entries on the user's calendar list.

Documentation: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list


var
rest: HCkRest;
success: Boolean;
oauth2: HCkOAuth2;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
kind: PWideChar;
etag: PWideChar;
nextSyncToken: PWideChar;
i: Integer;
count_i: Integer;
id: PWideChar;
summary: PWideChar;
timeZone: PWideChar;
colorId: PWideChar;
backgroundColor: PWideChar;
foregroundColor: PWideChar;
selected: Boolean;
accessRole: PWideChar;
primary: Boolean;
j: Integer;
count_j: Integer;
method: PWideChar;
minutes: Integer;
type: PWideChar;

begin
rest := CkRest_Create();

//   Provide a previously obtained OAuth2 access token.
oauth2 := CkOAuth2_Create();
CkOAuth2_putAccessToken(oauth2,'OAUTH2_ACCESS_TOKEN');
CkRest_SetAuthOAuth2(rest,oauth2);

success := CkRest_Connect(rest,'www.googleapis.com',443,True,True);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/calendar/v3/users/me/calendarList',sbJson);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Received error response code: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
    Memo1.Lines.Add('Response body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbJson));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbJson);

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.

kind := CkJsonObject__stringOf(json,'kind');
etag := CkJsonObject__stringOf(json,'etag');
nextSyncToken := CkJsonObject__stringOf(json,'nextSyncToken');
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'items');
while i < count_i do
  begin
CkJsonObject_putI(json,i);
    kind := CkJsonObject__stringOf(json,'items[i].kind');
    etag := CkJsonObject__stringOf(json,'items[i].etag');
    id := CkJsonObject__stringOf(json,'items[i].id');
    summary := CkJsonObject__stringOf(json,'items[i].summary');
    timeZone := CkJsonObject__stringOf(json,'items[i].timeZone');
    colorId := CkJsonObject__stringOf(json,'items[i].colorId');
    backgroundColor := CkJsonObject__stringOf(json,'items[i].backgroundColor');
    foregroundColor := CkJsonObject__stringOf(json,'items[i].foregroundColor');
    selected := CkJsonObject_BoolOf(json,'items[i].selected');
    accessRole := CkJsonObject__stringOf(json,'items[i].accessRole');
    primary := CkJsonObject_BoolOf(json,'items[i].primary');
    j := 0;
    count_j := CkJsonObject_SizeOfArray(json,'items[i].defaultReminders');
    while j < count_j do
      begin
CkJsonObject_putJ(json,j);
        method := CkJsonObject__stringOf(json,'items[i].defaultReminders[j].method');
        minutes := CkJsonObject_IntOf(json,'items[i].defaultReminders[j].minutes');
        j := j + 1;
      end;

    j := 0;
    count_j := CkJsonObject_SizeOfArray(json,'items[i].notificationSettings.notifications');
    while j < count_j do
      begin
CkJsonObject_putJ(json,j);
        type := CkJsonObject__stringOf(json,'items[i].notificationSettings.notifications[j].type');
        method := CkJsonObject__stringOf(json,'items[i].notificationSettings.notifications[j].method');
        j := j + 1;
      end;

    i := i + 1;
  end;

Memo1.Lines.Add('Example Completed.');

CkRest_Dispose(rest);
CkOAuth2_Dispose(oauth2);
CkStringBuilder_Dispose(sbJson);
CkJsonObject_Dispose(json);

Sample JSON Response Body

{
 "kind": "calendar#calendarList",
 "etag": "\"p33gdfl6bualde0g\"",
 "nextSyncToken": "CODX1MvyqtcCEhhzdXBwb3J0QGNoaWxrYXRjbG91ZC5jb20=",
 "items": [
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1465249947472000\"",
   "id": "support@chilkatcloud.com",
   "summary": "support@chilkatcloud.com",
   "timeZone": "America/Chicago",
   "colorId": "14",
   "backgroundColor": "#9fe1e7",
   "foregroundColor": "#000000",
   "selected": true,
   "accessRole": "owner",
   "defaultReminders": [
    {
     "method": "popup",
     "minutes": 10
    }
   ],
   "notificationSettings": {
    "notifications": [
     {
      "type": "eventCreation",
      "method": "email"
     },
     {
      "type": "eventChange",
      "method": "email"
     },
     {
      "type": "eventCancellation",
      "method": "email"
     },
     {
      "type": "eventResponse",
      "method": "email"
     }
    ]
   },
   "primary": true
  },
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1502373382732000\"",
   "id": "#contacts@group.v.calendar.google.com",
   "summary": "Contacts",
   "timeZone": "America/Chicago",
   "colorId": "13",
   "backgroundColor": "#92e1c0",
   "foregroundColor": "#000000",
   "selected": true,
   "accessRole": "reader",
   "defaultReminders": []
  },
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1502373376447000\"",
   "id": "en.usa#holiday@group.v.calendar.google.com",
   "summary": "Holidays in United States",
   "timeZone": "America/Chicago",
   "colorId": "8",
   "backgroundColor": "#16a765",
   "foregroundColor": "#000000",
   "selected": true,
   "accessRole": "reader",
   "defaultReminders": []
  }
 ]
}