Delphi DLL Google Calendar: Insert an Event

Back to Index

Inserts a new event to a specified calendar.
This example inserts an event into the calender specified by ID = "support@chilkatcloud.com"
Returns the JSON of the event that was created.

Documentation: https://developers.google.com/google-apps/calendar/v3/reference/events/insert


var
rest: HCkRest;
success: Boolean;
oauth2: HCkOAuth2;
jsonReq: HCkJsonObject;
sbReq: HCkStringBuilder;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
kind: PWideChar;
etag: PWideChar;
id: PWideChar;
status: PWideChar;
htmlLink: PWideChar;
created: PWideChar;
updated: PWideChar;
summary: PWideChar;
description: PWideChar;
location: PWideChar;
creatorEmail: PWideChar;
creatorSelf: Boolean;
organizerEmail: PWideChar;
organizerSelf: Boolean;
startDateTime: PWideChar;
endDateTime: PWideChar;
iCalUID: PWideChar;
sequence: Integer;
hangoutLink: PWideChar;
remindersUseDefault: Boolean;
i: Integer;
count_i: Integer;
email: PWideChar;
responseStatus: PWideChar;
organizer: Boolean;
self: Boolean;

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;

//  The following code creates the JSON request body.
//  The JSON created by this code is shown below.
jsonReq := CkJsonObject_Create();
CkJsonObject_UpdateString(jsonReq,'kind','calendar#event');
CkJsonObject_UpdateString(jsonReq,'status','confirmed');
CkJsonObject_UpdateString(jsonReq,'summary','QXQC');
CkJsonObject_UpdateString(jsonReq,'description','21st Annual QXQC Gathering');
CkJsonObject_UpdateString(jsonReq,'location','Quigley''s Irish Pub, 43 E Jefferson Ave, Naperville, IL 60540, USA');
CkJsonObject_UpdateString(jsonReq,'start.dateTime','2017-12-09T20:00:00-06:00');
CkJsonObject_UpdateString(jsonReq,'end.dateTime','2017-12-09T23:00:00-06:00');
CkJsonObject_UpdateString(jsonReq,'attendees[0].email','dean@example.com');
CkJsonObject_UpdateString(jsonReq,'attendees[0].responseStatus','needsAction');
CkJsonObject_UpdateString(jsonReq,'attendees[1].email','support@chilkatcloud.com');
CkJsonObject_UpdateBool(jsonReq,'attendees[1].organizer',True);
CkJsonObject_UpdateBool(jsonReq,'attendees[1].self',True);
CkJsonObject_UpdateString(jsonReq,'attendees[1].responseStatus','accepted');
CkJsonObject_UpdateString(jsonReq,'attendees[2].email','ajay@example.com');
CkJsonObject_UpdateString(jsonReq,'attendees[2].responseStatus','needsAction');
CkJsonObject_UpdateString(jsonReq,'attendees[3].email','jim@example.com');
CkJsonObject_UpdateString(jsonReq,'attendees[3].responseStatus','needsAction');
CkJsonObject_UpdateString(jsonReq,'attendees[4].email','gilian@example.com');
CkJsonObject_UpdateString(jsonReq,'attendees[4].responseStatus','needsAction');
CkJsonObject_UpdateBool(jsonReq,'reminders.useDefault',True);

sbReq := CkStringBuilder_Create();
CkJsonObject_EmitSb(jsonReq,sbReq);

CkRest_AddHeader(rest,'Content-Type','application/json');

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/calendar/v3/calendars/support@chilkatcloud.com/events',sbReq,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');
id := CkJsonObject__stringOf(json,'id');
status := CkJsonObject__stringOf(json,'status');
htmlLink := CkJsonObject__stringOf(json,'htmlLink');
created := CkJsonObject__stringOf(json,'created');
updated := CkJsonObject__stringOf(json,'updated');
summary := CkJsonObject__stringOf(json,'summary');
description := CkJsonObject__stringOf(json,'description');
location := CkJsonObject__stringOf(json,'location');
creatorEmail := CkJsonObject__stringOf(json,'creator.email');
creatorSelf := CkJsonObject_BoolOf(json,'creator.self');
organizerEmail := CkJsonObject__stringOf(json,'organizer.email');
organizerSelf := CkJsonObject_BoolOf(json,'organizer.self');
startDateTime := CkJsonObject__stringOf(json,'start.dateTime');
endDateTime := CkJsonObject__stringOf(json,'end.dateTime');
iCalUID := CkJsonObject__stringOf(json,'iCalUID');
sequence := CkJsonObject_IntOf(json,'sequence');
hangoutLink := CkJsonObject__stringOf(json,'hangoutLink');
remindersUseDefault := CkJsonObject_BoolOf(json,'reminders.useDefault');
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'attendees');
while i < count_i do
  begin
CkJsonObject_putI(json,i);
    email := CkJsonObject__stringOf(json,'attendees[i].email');
    responseStatus := CkJsonObject__stringOf(json,'attendees[i].responseStatus');
    organizer := CkJsonObject_BoolOf(json,'attendees[i].organizer');
    self := CkJsonObject_BoolOf(json,'attendees[i].self');
    i := i + 1;
  end;

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

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

Sample JSON Request Body

{
  "kind": "calendar#event",
  "status": "confirmed",
  "summary": "QXQC",
  "description": "21st Annual QXQC Gathering",
  "location": "Quigley's Irish Pub, 43 E Jefferson Ave, Naperville, IL 60540, USA",
  "start": {
    "dateTime": "2017-12-09T20:00:00-06:00"
  },
  "end": {
    "dateTime": "2017-12-09T23:00:00-06:00"
  },
  "attendees": [
    {
      "email": "dean@example.com",
      "responseStatus": "needsAction"
    },
    {
      "email": "support@chilkatcloud.com",
      "organizer": true,
      "self": true,
      "responseStatus": "accepted"
    },
    {
      "email": "ajay@example.com",
      "responseStatus": "needsAction"
    },
    {
      "email": "jim@example.com",
      "responseStatus": "needsAction"
    },
    {
      "email": "gilian@example.com",
      "responseStatus": "needsAction"
    }
  ],
  "reminders": {
    "useDefault": true
  }
}

Sample JSON Response Body

{
  "kind": "calendar#event",
  "etag": "\"3020332888490000\"",
  "id": "oqel9ijr6rgfgqg12b0qtlhfjs",
  "status": "confirmed",
  "htmlLink": "https://www.google.com/calendar/event?eid=b3FlbDlpanI2cmdmZ3FnMTJiMHF0bGhmanMgc3VwcG9ydEBjaGlsa2F0Y2xvdWQuY29t",
  "created": "2017-11-08T18:40:44.000Z",
  "updated": "2017-11-08T18:40:44.245Z",
  "summary": "QXQC",
  "description": "21st Annual QXQC Gathering",
  "location": "Quigley's Irish Pub, 43 E Jefferson Ave, Naperville, IL 60540, USA",
  "creator": {
    "email": "support@chilkatcloud.com",
    "self": true
  },
  "organizer": {
    "email": "support@chilkatcloud.com",
    "self": true
  },
  "start": {
    "dateTime": "2017-12-09T20:00:00-06:00"
  },
  "end": {
    "dateTime": "2017-12-09T23:00:00-06:00"
  },
  "iCalUID": "oqel9ijr6rgfgqg12b0qtlhfjs@google.com",
  "sequence": 0,
  "attendees": [
    {
      "email": "dean@example.com",
      "responseStatus": "needsAction"
    },
    {
      "email": "support@chilkatcloud.com",
      "organizer": true,
      "self": true,
      "responseStatus": "accepted"
    },
    {
      "email": "ajay@example.com",
      "responseStatus": "needsAction"
    },
    {
      "email": "jim@example.com",
      "responseStatus": "needsAction"
    },
    {
      "email": "gilian@example.com",
      "responseStatus": "needsAction"
    }
  ],
  "hangoutLink": "https://plus.google.com/hangouts/_/chilkatcloud.com/qxqc?hceid=c3VwcG9ydEBjaGlsa2F0Y2xvdWQuY29t.oqel9ijr6rgfgqg12b0qtlhfjs",
  "reminders": {
    "useDefault": true
  }
}