PureBasic Google Calendar: Get Calendar

Back to Index

Returns an entry on the user's calendar list.
This example gets the calendar having the ID = "support@chilkatcloud.com".

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


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/support@chilkatcloud.com",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
    id.s
    summary.s
    timeZone.s
    colorId.s
    backgroundColor.s
    foregroundColor.s
    selected.i
    accessRole.s
    primary.i
    i.i
    count_i.i
    method.s
    minutes.i
    type.s

    kind = CkJsonObject::ckStringOf(json,"kind")
    etag = CkJsonObject::ckStringOf(json,"etag")
    id = CkJsonObject::ckStringOf(json,"id")
    summary = CkJsonObject::ckStringOf(json,"summary")
    timeZone = CkJsonObject::ckStringOf(json,"timeZone")
    colorId = CkJsonObject::ckStringOf(json,"colorId")
    backgroundColor = CkJsonObject::ckStringOf(json,"backgroundColor")
    foregroundColor = CkJsonObject::ckStringOf(json,"foregroundColor")
    selected = CkJsonObject::ckBoolOf(json,"selected")
    accessRole = CkJsonObject::ckStringOf(json,"accessRole")
    primary = CkJsonObject::ckBoolOf(json,"primary")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"defaultReminders")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        method = CkJsonObject::ckStringOf(json,"defaultReminders[i].method")
        minutes = CkJsonObject::ckIntOf(json,"defaultReminders[i].minutes")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"notificationSettings.notifications")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        type = CkJsonObject::ckStringOf(json,"notificationSettings.notifications[i].type")
        method = CkJsonObject::ckStringOf(json,"notificationSettings.notifications[i].method")
        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#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
}