C 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


#include <C_CkRest.h>
#include <C_CkOAuth2.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    HCkOAuth2 oauth2;
    HCkJsonObject jsonReq;
    HCkStringBuilder sbReq;
    HCkStringBuilder sbJson;
    HCkJsonObject json;
    const char *kind;
    const char *etag;
    const char *id;
    const char *status;
    const char *htmlLink;
    const char *created;
    const char *updated;
    const char *summary;
    const char *creatorEmail;
    BOOL creatorSelf;
    const char *organizerEmail;
    BOOL organizerSelf;
    const char *startDateTime;
    const char *endDateTime;
    const char *iCalUID;
    int sequence;
    const char *hangoutLink;
    BOOL remindersUseDefault;

    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) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        return;
    }

    //  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) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        CkJsonObject_Dispose(jsonReq);
        CkStringBuilder_Dispose(sbReq);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("Received error response code: %d\n",CkRest_getResponseStatusCode(rest));
        printf("Response body:\n");
        printf("%s\n",CkStringBuilder_getAsString(sbJson));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        CkJsonObject_Dispose(jsonReq);
        CkStringBuilder_Dispose(sbReq);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    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");

    printf("Example Completed.\n");


    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
  }
}