Tcl Google Calendar: Change the Color of an Event

Back to Index

Changes the color of a particular event. The calendar and event are specified by ID. This example changes the color for an event (id = 52e7uk7j7vl5dosj7b25memov0) on the calendar (id = support@chilkatcloud.com).
This changes the event to colorId = "6" which seems to be an orange-ish color..

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



load ./chilkat.dll

set rest [new_CkRest]

#   Provide a previously obtained OAuth2 access token.
set oauth2 [new_CkOAuth2]

CkOAuth2_put_AccessToken $oauth2 "OAUTH2_ACCESS_TOKEN"
CkRest_SetAuthOAuth2 $rest $oauth2

set success [CkRest_Connect $rest "www.googleapis.com" 443 1 1]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    exit
}

#  The following code creates the JSON request body.
#  The JSON created by this code is shown below.
set jsonReq [new_CkJsonObject]

CkJsonObject_UpdateString $jsonReq "colorId" "6"

set sbReq [new_CkStringBuilder]

CkJsonObject_EmitSb $jsonReq $sbReq

CkRest_AddHeader $rest "Content-Type" "application/json"

set sbJson [new_CkStringBuilder]

set success [CkRest_FullRequestSb $rest "PATCH" "/calendar/v3/calendars/support@chilkatcloud.com/events/52e7uk7j7vl5dosj7b25memov0" $sbReq $sbJson]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    delete_CkJsonObject $jsonReq
    delete_CkStringBuilder $sbReq
    delete_CkStringBuilder $sbJson
    exit
}

if {[expr [CkRest_ResponseStatusCode $rest] != 200]} then {
    puts "Received error response code: [CkRest_ResponseStatusCode $rest]"
    puts "Response body:"
    puts [CkStringBuilder_getAsString $sbJson]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    delete_CkJsonObject $jsonReq
    delete_CkStringBuilder $sbReq
    delete_CkStringBuilder $sbJson
    exit
}

set json [new_CkJsonObject]

CkJsonObject_LoadSb $json $sbJson

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

set kind [CkJsonObject_stringOf $json "kind"]
set etag [CkJsonObject_stringOf $json "etag"]
set id [CkJsonObject_stringOf $json "id"]
set status [CkJsonObject_stringOf $json "status"]
set htmlLink [CkJsonObject_stringOf $json "htmlLink"]
set created [CkJsonObject_stringOf $json "created"]
set updated [CkJsonObject_stringOf $json "updated"]
set summary [CkJsonObject_stringOf $json "summary"]
set colorId [CkJsonObject_stringOf $json "colorId"]
set creatorEmail [CkJsonObject_stringOf $json "creator.email"]
set creatorSelf [CkJsonObject_BoolOf $json "creator.self"]
set organizerEmail [CkJsonObject_stringOf $json "organizer.email"]
set organizerSelf [CkJsonObject_BoolOf $json "organizer.self"]
set startDateTime [CkJsonObject_stringOf $json "start.dateTime"]
set endDateTime [CkJsonObject_stringOf $json "end.dateTime"]
set iCalUID [CkJsonObject_stringOf $json "iCalUID"]
set sequence [CkJsonObject_IntOf $json "sequence"]
set hangoutLink [CkJsonObject_stringOf $json "hangoutLink"]
set remindersUseDefault [CkJsonObject_BoolOf $json "reminders.useDefault"]

puts "Example Completed."

delete_CkRest $rest
delete_CkOAuth2 $oauth2
delete_CkJsonObject $jsonReq
delete_CkStringBuilder $sbReq
delete_CkStringBuilder $sbJson
delete_CkJsonObject $json

Sample JSON Request Body

{
    "colorId": "6"
}

Sample JSON Response Body

{
  "kind": "calendar#event",
  "etag": "\"3021538600038000\"",
  "id": "52e7uk7j7vl5dosj7b25memov0",
  "status": "confirmed",
  "htmlLink": "https://www.google.com/calendar/event?eid=NTJlN3VrN2o3dmw1ZG9zajdiMjVtZW1vdjAgc3VwcG9ydEBjaGlsa2F0Y2xvdWQuY29t",
  "created": "2017-11-08T21:27:50.000Z",
  "updated": "2017-11-15T18:08:20.019Z",
  "summary": "Dentist Appointment",
  "colorId": "6",
  "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
  }
}