Delphi DLL Google Calendar: Update (Patch) an Event

Back to Index

Updates an event. (This example modifies the start/end date/time for the event id 52e7uk7j7vl5dosj7b25memov0 in the calendar with id = "support@chilkatcloud.com".)

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


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;
creatorEmail: PWideChar;
creatorSelf: Boolean;
organizerEmail: PWideChar;
organizerSelf: Boolean;
startDateTime: PWideChar;
endDateTime: PWideChar;
iCalUID: PWideChar;
sequence: Integer;
hangoutLink: PWideChar;
remindersUseDefault: 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,'start.dateTime','2017-11-27T10:30:00-06:00');
CkJsonObject_UpdateString(jsonReq,'end.dateTime','2017-11-27T11:30:00-06:00');

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

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

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'PATCH','/calendar/v3/calendars/support@chilkatcloud.com/events/52e7uk7j7vl5dosj7b25memov0',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');
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');

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

{
  "start": {
    "dateTime": "2017-11-27T10:30:00-06:00"
  },
  "end": {
    "dateTime": "2017-11-27T11:30:00-06:00"
  }
}

Sample JSON Response Body

{
  "kind": "calendar#event",
  "etag": "\"3020353646374000\"",
  "id": "52e7uk7j7vl5dosj7b25memov0",
  "status": "confirmed",
  "htmlLink": "https://www.google.com/calendar/event?eid=NTJlN3VrN2o3dmw1ZG9zajdiMjVtZW1vdjAgc3VwcG9ydEBjaGlsa2F0Y2xvdWQuY29t",
  "created": "2017-11-08T21:27:50.000Z",
  "updated": "2017-11-08T21:33:43.187Z",
  "summary": "Dentist Appointment",
  "creator": {
    "email": "support@chilkatcloud.com",
    "self": true
  },
  "organizer": {
    "email": "support@chilkatcloud.com",
    "self": true
  },
  "start": {
    "dateTime": "2017-11-27T10:30:00-06:00"
  },
  "end": {
    "dateTime": "2017-11-27T11:30:00-06:00"
  },
  "iCalUID": "52e7uk7j7vl5dosj7b25memov0@google.com",
  "sequence": 1,
  "hangoutLink": "https://plus.google.com/hangouts/_/chilkatcloud.com/support?hceid=c3VwcG9ydEBjaGlsa2F0Y2xvdWQuY29t.52e7uk7j7vl5dosj7b25memov0",
  "reminders": {
    "useDefault": true
  }
}