Delphi DLL 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


var
rest: HCkRest;
success: Boolean;
oauth2: HCkOAuth2;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
i: Integer;
count_i: Integer;
kind: PWideChar;
id: PWideChar;
createdTime: PWideChar;
modifiedTime: PWideChar;
authorKind: PWideChar;
authorDisplayName: PWideChar;
authorPhotoLink: PWideChar;
authorMe: Boolean;
htmlContent: PWideChar;
content: PWideChar;
deleted: Boolean;
resolved: Boolean;
j: Integer;
count_j: Integer;
action: PWideChar;

begin
rest := CkRest_Create();

//   Provide a previously obtained OAuth2 access token.
oauth2 := CkOAuth2_Create();
CkOAuth2_putAccessToken(oauth2,'OAUTH2_ACCESS_TOKEN');
CkRest_SetAuthOAuth2(rest,oauth2);

success := CkRest_Connect(rest,'www.googleapis.com',443,True,True);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_AddQueryParam(rest,'fields','comments');

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/drive/v3/files/0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw/comments',sbJson);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Received error response code: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
    Memo1.Lines.Add('Response body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbJson));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbJson);

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

i := 0;
count_i := CkJsonObject_SizeOfArray(json,'comments');
while i < count_i do
  begin
CkJsonObject_putI(json,i);
    kind := CkJsonObject__stringOf(json,'comments[i].kind');
    id := CkJsonObject__stringOf(json,'comments[i].id');
    createdTime := CkJsonObject__stringOf(json,'comments[i].createdTime');
    modifiedTime := CkJsonObject__stringOf(json,'comments[i].modifiedTime');
    authorKind := CkJsonObject__stringOf(json,'comments[i].author.kind');
    authorDisplayName := CkJsonObject__stringOf(json,'comments[i].author.displayName');
    authorPhotoLink := CkJsonObject__stringOf(json,'comments[i].author.photoLink');
    authorMe := CkJsonObject_BoolOf(json,'comments[i].author.me');
    htmlContent := CkJsonObject__stringOf(json,'comments[i].htmlContent');
    content := CkJsonObject__stringOf(json,'comments[i].content');
    deleted := CkJsonObject_BoolOf(json,'comments[i].deleted');
    resolved := CkJsonObject_BoolOf(json,'comments[i].resolved');
    j := 0;
    count_j := CkJsonObject_SizeOfArray(json,'comments[i].replies');
    while j < count_j do
      begin
CkJsonObject_putJ(json,j);
        kind := CkJsonObject__stringOf(json,'comments[i].replies[j].kind');
        id := CkJsonObject__stringOf(json,'comments[i].replies[j].id');
        createdTime := CkJsonObject__stringOf(json,'comments[i].replies[j].createdTime');
        modifiedTime := CkJsonObject__stringOf(json,'comments[i].replies[j].modifiedTime');
        authorKind := CkJsonObject__stringOf(json,'comments[i].replies[j].author.kind');
        authorDisplayName := CkJsonObject__stringOf(json,'comments[i].replies[j].author.displayName');
        authorPhotoLink := CkJsonObject__stringOf(json,'comments[i].replies[j].author.photoLink');
        authorMe := CkJsonObject_BoolOf(json,'comments[i].replies[j].author.me');
        htmlContent := CkJsonObject__stringOf(json,'comments[i].replies[j].htmlContent');
        content := CkJsonObject__stringOf(json,'comments[i].replies[j].content');
        deleted := CkJsonObject_BoolOf(json,'comments[i].replies[j].deleted');
        action := CkJsonObject__stringOf(json,'comments[i].replies[j].action');
        j := j + 1;
      end;

    i := i + 1;
  end;

Memo1.Lines.Add('Example Completed.');

CkRest_Dispose(rest);
CkOAuth2_Dispose(oauth2);
CkStringBuilder_Dispose(sbJson);
CkJsonObject_Dispose(json);

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