Unicode C 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


#include <C_CkRestW.h>
#include <C_CkOAuth2W.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    HCkOAuth2W oauth2;
    HCkJsonObjectW jsonReq;
    HCkStringBuilderW sbReq;
    HCkStringBuilderW sbJson;
    HCkJsonObjectW json;
    const wchar_t *kind;
    const wchar_t *etag;
    const wchar_t *id;
    const wchar_t *status;
    const wchar_t *htmlLink;
    const wchar_t *created;
    const wchar_t *updated;
    const wchar_t *summary;
    const wchar_t *description;
    const wchar_t *location;
    const wchar_t *creatorEmail;
    BOOL creatorSelf;
    const wchar_t *organizerEmail;
    BOOL organizerSelf;
    const wchar_t *startDateTime;
    const wchar_t *endDateTime;
    const wchar_t *iCalUID;
    int sequence;
    const wchar_t *hangoutLink;
    BOOL remindersUseDefault;
    int i;
    int count_i;
    const wchar_t *email;
    const wchar_t *responseStatus;
    BOOL organizer;
    BOOL self;

    rest = CkRestW_Create();

    //   Provide a previously obtained OAuth2 access token.
    oauth2 = CkOAuth2W_Create();
    CkOAuth2W_putAccessToken(oauth2,L"OAUTH2_ACCESS_TOKEN");
    CkRestW_SetAuthOAuth2(rest,oauth2);

    success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,TRUE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        return;
    }

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

    sbReq = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(jsonReq,sbReq);

    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");

    sbJson = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/calendar/v3/calendars/support@chilkatcloud.com/events",sbReq,sbJson);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        CkJsonObjectW_Dispose(jsonReq);
        CkStringBuilderW_Dispose(sbReq);
        CkStringBuilderW_Dispose(sbJson);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"Received error response code: %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbJson));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        CkJsonObjectW_Dispose(jsonReq);
        CkStringBuilderW_Dispose(sbReq);
        CkStringBuilderW_Dispose(sbJson);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(json,sbJson);

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

    kind = CkJsonObjectW_stringOf(json,L"kind");
    etag = CkJsonObjectW_stringOf(json,L"etag");
    id = CkJsonObjectW_stringOf(json,L"id");
    status = CkJsonObjectW_stringOf(json,L"status");
    htmlLink = CkJsonObjectW_stringOf(json,L"htmlLink");
    created = CkJsonObjectW_stringOf(json,L"created");
    updated = CkJsonObjectW_stringOf(json,L"updated");
    summary = CkJsonObjectW_stringOf(json,L"summary");
    description = CkJsonObjectW_stringOf(json,L"description");
    location = CkJsonObjectW_stringOf(json,L"location");
    creatorEmail = CkJsonObjectW_stringOf(json,L"creator.email");
    creatorSelf = CkJsonObjectW_BoolOf(json,L"creator.self");
    organizerEmail = CkJsonObjectW_stringOf(json,L"organizer.email");
    organizerSelf = CkJsonObjectW_BoolOf(json,L"organizer.self");
    startDateTime = CkJsonObjectW_stringOf(json,L"start.dateTime");
    endDateTime = CkJsonObjectW_stringOf(json,L"end.dateTime");
    iCalUID = CkJsonObjectW_stringOf(json,L"iCalUID");
    sequence = CkJsonObjectW_IntOf(json,L"sequence");
    hangoutLink = CkJsonObjectW_stringOf(json,L"hangoutLink");
    remindersUseDefault = CkJsonObjectW_BoolOf(json,L"reminders.useDefault");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"attendees");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        email = CkJsonObjectW_stringOf(json,L"attendees[i].email");
        responseStatus = CkJsonObjectW_stringOf(json,L"attendees[i].responseStatus");
        organizer = CkJsonObjectW_BoolOf(json,L"attendees[i].organizer");
        self = CkJsonObjectW_BoolOf(json,L"attendees[i].self");
        i = i + 1;
    }

    wprintf(L"Example Completed.\n");


    CkRestW_Dispose(rest);
    CkOAuth2W_Dispose(oauth2);
    CkJsonObjectW_Dispose(jsonReq);
    CkStringBuilderW_Dispose(sbReq);
    CkStringBuilderW_Dispose(sbJson);
    CkJsonObjectW_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
  }
}