Swift 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



func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //   Provide a previously obtained OAuth2 access token.
    let oauth2 = CkoOAuth2()
    oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
    rest.SetAuthOAuth2(oauth2)

    success = rest.Connect("www.googleapis.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

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

    let sbReq = CkoStringBuilder()
    jsonReq.EmitSb(sbReq)

    rest.AddHeader("Content-Type", value: "application/json")

    let sbJson = CkoStringBuilder()
    success = rest.FullRequestSb("POST", uriPath: "/calendar/v3/calendars/support@chilkatcloud.com/events", requestBody: sbReq, responseBody: sbJson)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    if rest.ResponseStatusCode.integerValue != 200 {
        print("Received error response code: \(rest.ResponseStatusCode.integerValue)")
        print("Response body:")
        print("\(sbJson.GetAsString())")
        return
    }

    let json = CkoJsonObject()
    json.LoadSb(sbJson)

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    var kind: String?
    var etag: String?
    var id: String?
    var status: String?
    var htmlLink: String?
    var created: String?
    var updated: String?
    var summary: String?
    var description: String?
    var location: String?
    var creatorEmail: String?
    var creatorSelf: Bool
    var organizerEmail: String?
    var organizerSelf: Bool
    var startDateTime: String?
    var endDateTime: String?
    var iCalUID: String?
    var sequence: Int
    var hangoutLink: String?
    var remindersUseDefault: Bool
    var i: Int
    var count_i: Int
    var email: String?
    var responseStatus: String?
    var organizer: Bool
    var self: Bool

    kind = json.StringOf("kind")
    etag = json.StringOf("etag")
    id = json.StringOf("id")
    status = json.StringOf("status")
    htmlLink = json.StringOf("htmlLink")
    created = json.StringOf("created")
    updated = json.StringOf("updated")
    summary = json.StringOf("summary")
    description = json.StringOf("description")
    location = json.StringOf("location")
    creatorEmail = json.StringOf("creator.email")
    creatorSelf = json.BoolOf("creator.self")
    organizerEmail = json.StringOf("organizer.email")
    organizerSelf = json.BoolOf("organizer.self")
    startDateTime = json.StringOf("start.dateTime")
    endDateTime = json.StringOf("end.dateTime")
    iCalUID = json.StringOf("iCalUID")
    sequence = json.IntOf("sequence").integerValue
    hangoutLink = json.StringOf("hangoutLink")
    remindersUseDefault = json.BoolOf("reminders.useDefault")
    i = 0
    count_i = json.SizeOfArray("attendees").integerValue
    while i < count_i {
        json.I = i
        email = json.StringOf("attendees[i].email")
        responseStatus = json.StringOf("attendees[i].responseStatus")
        organizer = json.BoolOf("attendees[i].organizer")
        self = json.BoolOf("attendees[i].self")
        i = i + 1
    }

    print("Example Completed.")

}

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