PureBasic Google Drive: List Comments on a File

Back to Index

Lists the comment on a file. The file is specified by file ID. This example lists comments on the file having id = "0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw".

Documentation: https://developers.google.com/drive/v3/reference/comments/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

    CkRest::ckAddQueryParam(rest,"fields","comments")

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

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/drive/v3/files/0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw/comments",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.
    i.i
    count_i.i
    kind.s
    id.s
    createdTime.s
    modifiedTime.s
    authorKind.s
    authorDisplayName.s
    authorPhotoLink.s
    authorMe.i
    htmlContent.s
    content.s
    deleted.i
    resolved.i
    j.i
    count_j.i
    action.s

    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"comments")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        kind = CkJsonObject::ckStringOf(json,"comments[i].kind")
        id = CkJsonObject::ckStringOf(json,"comments[i].id")
        createdTime = CkJsonObject::ckStringOf(json,"comments[i].createdTime")
        modifiedTime = CkJsonObject::ckStringOf(json,"comments[i].modifiedTime")
        authorKind = CkJsonObject::ckStringOf(json,"comments[i].author.kind")
        authorDisplayName = CkJsonObject::ckStringOf(json,"comments[i].author.displayName")
        authorPhotoLink = CkJsonObject::ckStringOf(json,"comments[i].author.photoLink")
        authorMe = CkJsonObject::ckBoolOf(json,"comments[i].author.me")
        htmlContent = CkJsonObject::ckStringOf(json,"comments[i].htmlContent")
        content = CkJsonObject::ckStringOf(json,"comments[i].content")
        deleted = CkJsonObject::ckBoolOf(json,"comments[i].deleted")
        resolved = CkJsonObject::ckBoolOf(json,"comments[i].resolved")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"comments[i].replies")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            kind = CkJsonObject::ckStringOf(json,"comments[i].replies[j].kind")
            id = CkJsonObject::ckStringOf(json,"comments[i].replies[j].id")
            createdTime = CkJsonObject::ckStringOf(json,"comments[i].replies[j].createdTime")
            modifiedTime = CkJsonObject::ckStringOf(json,"comments[i].replies[j].modifiedTime")
            authorKind = CkJsonObject::ckStringOf(json,"comments[i].replies[j].author.kind")
            authorDisplayName = CkJsonObject::ckStringOf(json,"comments[i].replies[j].author.displayName")
            authorPhotoLink = CkJsonObject::ckStringOf(json,"comments[i].replies[j].author.photoLink")
            authorMe = CkJsonObject::ckBoolOf(json,"comments[i].replies[j].author.me")
            htmlContent = CkJsonObject::ckStringOf(json,"comments[i].replies[j].htmlContent")
            content = CkJsonObject::ckStringOf(json,"comments[i].replies[j].content")
            deleted = CkJsonObject::ckBoolOf(json,"comments[i].replies[j].deleted")
            action = CkJsonObject::ckStringOf(json,"comments[i].replies[j].action")
            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

{
  "comments": [
    {
      "kind": "drive#comment",
      "id": "AAAABg7vWfw",
      "createdTime": "2017-11-13T17:51:57.906Z",
      "modifiedTime": "2017-11-13T17:51:57.906Z",
      "author": {
        "kind": "drive#user",
        "displayName": "Matt Fausey",
        "photoLink": "//ssl.gstatic.com/s2/profiles/images/silhouette96.png",
        "me": true
      },
      "htmlContent": "This is the 2nd test comment about this file...",
      "content": "This is the 2nd test comment about this file...",
      "deleted": false,
      "resolved": false,
      "replies": [
      ]
    },
    {
      "kind": "drive#comment",
      "id": "AAAABg7tSGw",
      "createdTime": "2017-11-13T17:43:05.735Z",
      "modifiedTime": "2017-11-13T18:25:11.828Z",
      "author": {
        "kind": "drive#user",
        "displayName": "Matt Fausey",
        "photoLink": "//ssl.gstatic.com/s2/profiles/images/silhouette96.png",
        "me": true
      },
      "htmlContent": "This is a test comment about this file...",
      "content": "This is a test comment about this file...",
      "deleted": false,
      "resolved": true,
      "replies": [
        {
          "kind": "drive#reply",
          "id": "AAAABg76H9g",
          "createdTime": "2017-11-13T18:24:12.782Z",
          "modifiedTime": "2017-11-13T18:24:12.782Z",
          "author": {
            "kind": "drive#user",
            "displayName": "Matt Fausey",
            "photoLink": "//ssl.gstatic.com/s2/profiles/images/silhouette96.png",
            "me": true
          },
          "htmlContent": "This is the first reply to a comment.",
          "content": "This is the first reply to a comment.",
          "deleted": false,
          "action": "reopen"
        },
        {
          "kind": "drive#reply",
          "id": "AAAABg76H9k",
          "createdTime": "2017-11-13T18:24:45.085Z",
          "modifiedTime": "2017-11-13T18:24:45.085Z",
          "author": {
            "kind": "drive#user",
            "displayName": "Matt Fausey",
            "photoLink": "//ssl.gstatic.com/s2/profiles/images/silhouette96.png",
            "me": true
          },
          "htmlContent": "This is the second reply to a comment.",
          "content": "This is the second reply to a comment.",
          "deleted": false,
          "action": "resolve"
        },
        {
          "kind": "drive#reply",
          "id": "AAAABg76H9o",
          "createdTime": "2017-11-13T18:25:11.828Z",
          "modifiedTime": "2017-11-13T18:25:11.828Z",
          "author": {
            "kind": "drive#user",
            "displayName": "Matt Fausey",
            "photoLink": "//ssl.gstatic.com/s2/profiles/images/silhouette96.png",
            "me": true
          },
          "htmlContent": "This is the third reply to a comment.",
          "content": "This is the third reply to a comment.",
          "deleted": false,
          "action": "resolve"
        }
      ]
    }
  ]
}