Swift Google Calendar: Create (Insert) a Calendar

Back to Index

Creates a new calendar. (i.e. Adds an entry to the user's calendar list.)

Documentation: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/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("summary", value: "test calendar abc")

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

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

    let sbJson = CkoStringBuilder()
    success = rest.FullRequestSb("POST", uriPath: "/calendar/v3/calendars", 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 summary: String?

    kind = json.StringOf("kind")
    etag = json.StringOf("etag")
    id = json.StringOf("id")
    summary = json.StringOf("summary")

    print("Example Completed.")

}

Sample JSON Request Body

  {
   "summary": "test calendar abc"
  }
  

Sample JSON Response Body

{
  "kind": "calendar#calendar",
  "etag": "\"BKjXnZpXR6E5ueFWG3UJk-2POYg/jb82ZwCZzTu1P_p3WnVVxDoJTeI\"",
  "id": "chilkatcloud.com_he3bfm0ljrl7p427u8vjmg9afo@group.calendar.google.com",
  "summary": "test calendar abc"
}