Tcl Google Calendar: List Calendars

Back to Index

Returns entries on the user's calendar list.

Documentation: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list



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
}

set sbJson [new_CkStringBuilder]

set success [CkRest_FullRequestNoBodySb $rest "GET" "/calendar/v3/users/me/calendarList" $sbJson]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    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_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 nextSyncToken [CkJsonObject_stringOf $json "nextSyncToken"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "items"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set kind [CkJsonObject_stringOf $json "items[i].kind"]
    set etag [CkJsonObject_stringOf $json "items[i].etag"]
    set id [CkJsonObject_stringOf $json "items[i].id"]
    set summary [CkJsonObject_stringOf $json "items[i].summary"]
    set timeZone [CkJsonObject_stringOf $json "items[i].timeZone"]
    set colorId [CkJsonObject_stringOf $json "items[i].colorId"]
    set backgroundColor [CkJsonObject_stringOf $json "items[i].backgroundColor"]
    set foregroundColor [CkJsonObject_stringOf $json "items[i].foregroundColor"]
    set selected [CkJsonObject_BoolOf $json "items[i].selected"]
    set accessRole [CkJsonObject_stringOf $json "items[i].accessRole"]
    set primary [CkJsonObject_BoolOf $json "items[i].primary"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "items[i].defaultReminders"]
    while {[expr $j < $count_j]} {
        CkJsonObject_put_J $json $j
        set method [CkJsonObject_stringOf $json "items[i].defaultReminders[j].method"]
        set minutes [CkJsonObject_IntOf $json "items[i].defaultReminders[j].minutes"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "items[i].notificationSettings.notifications"]
    while {[expr $j < $count_j]} {
        CkJsonObject_put_J $json $j
        set type [CkJsonObject_stringOf $json "items[i].notificationSettings.notifications[j].type"]
        set method [CkJsonObject_stringOf $json "items[i].notificationSettings.notifications[j].method"]
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

puts "Example Completed."

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

Sample JSON Response Body

{
 "kind": "calendar#calendarList",
 "etag": "\"p33gdfl6bualde0g\"",
 "nextSyncToken": "CODX1MvyqtcCEhhzdXBwb3J0QGNoaWxrYXRjbG91ZC5jb20=",
 "items": [
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1465249947472000\"",
   "id": "support@chilkatcloud.com",
   "summary": "support@chilkatcloud.com",
   "timeZone": "America/Chicago",
   "colorId": "14",
   "backgroundColor": "#9fe1e7",
   "foregroundColor": "#000000",
   "selected": true,
   "accessRole": "owner",
   "defaultReminders": [
    {
     "method": "popup",
     "minutes": 10
    }
   ],
   "notificationSettings": {
    "notifications": [
     {
      "type": "eventCreation",
      "method": "email"
     },
     {
      "type": "eventChange",
      "method": "email"
     },
     {
      "type": "eventCancellation",
      "method": "email"
     },
     {
      "type": "eventResponse",
      "method": "email"
     }
    ]
   },
   "primary": true
  },
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1502373382732000\"",
   "id": "#contacts@group.v.calendar.google.com",
   "summary": "Contacts",
   "timeZone": "America/Chicago",
   "colorId": "13",
   "backgroundColor": "#92e1c0",
   "foregroundColor": "#000000",
   "selected": true,
   "accessRole": "reader",
   "defaultReminders": []
  },
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1502373376447000\"",
   "id": "en.usa#holiday@group.v.calendar.google.com",
   "summary": "Holidays in United States",
   "timeZone": "America/Chicago",
   "colorId": "8",
   "backgroundColor": "#16a765",
   "foregroundColor": "#000000",
   "selected": true,
   "accessRole": "reader",
   "defaultReminders": []
  }
 ]
}