PureBasic 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


IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkOAuth2.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;   Provide a previously obtained OAuth2 access token.
    oauth2.i = CkOAuth2::ckCreate()
    If oauth2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkOAuth2::setCkAccessToken(oauth2, "OAUTH2_ACCESS_TOKEN")
    CkRest::ckSetAuthOAuth2(rest,oauth2)

    success = CkRest::ckConnect(rest,"www.googleapis.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    sbJson.i = CkStringBuilder::ckCreate()
    If sbJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/calendar/v3/users/me/calendarList",sbJson)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response body:"
        Debug CkStringBuilder::ckGetAsString(sbJson)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbJson)

    ;  The following code parses the JSON response.
    ;  A sample JSON response is shown below the sample code.
    kind.s
    etag.s
    nextSyncToken.s
    i.i
    count_i.i
    id.s
    summary.s
    timeZone.s
    colorId.s
    backgroundColor.s
    foregroundColor.s
    selected.i
    accessRole.s
    primary.i
    j.i
    count_j.i
    method.s
    minutes.i
    type.s

    kind = CkJsonObject::ckStringOf(json,"kind")
    etag = CkJsonObject::ckStringOf(json,"etag")
    nextSyncToken = CkJsonObject::ckStringOf(json,"nextSyncToken")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"items")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        kind = CkJsonObject::ckStringOf(json,"items[i].kind")
        etag = CkJsonObject::ckStringOf(json,"items[i].etag")
        id = CkJsonObject::ckStringOf(json,"items[i].id")
        summary = CkJsonObject::ckStringOf(json,"items[i].summary")
        timeZone = CkJsonObject::ckStringOf(json,"items[i].timeZone")
        colorId = CkJsonObject::ckStringOf(json,"items[i].colorId")
        backgroundColor = CkJsonObject::ckStringOf(json,"items[i].backgroundColor")
        foregroundColor = CkJsonObject::ckStringOf(json,"items[i].foregroundColor")
        selected = CkJsonObject::ckBoolOf(json,"items[i].selected")
        accessRole = CkJsonObject::ckStringOf(json,"items[i].accessRole")
        primary = CkJsonObject::ckBoolOf(json,"items[i].primary")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"items[i].defaultReminders")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            method = CkJsonObject::ckStringOf(json,"items[i].defaultReminders[j].method")
            minutes = CkJsonObject::ckIntOf(json,"items[i].defaultReminders[j].minutes")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"items[i].notificationSettings.notifications")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            type = CkJsonObject::ckStringOf(json,"items[i].notificationSettings.notifications[j].type")
            method = CkJsonObject::ckStringOf(json,"items[i].notificationSettings.notifications[j].method")
            j = j + 1
        Wend
        i = i + 1
    Wend

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkOAuth2::ckDispose(oauth2)
    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure

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": []
  }
 ]
}