Node.js 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 os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    //   Provide a previously obtained OAuth2 access token.
    var oauth2 = new chilkat.OAuth2();
    oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN";
    rest.SetAuthOAuth2(oauth2);

    success = rest.Connect("www.googleapis.com",443,true,true);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    rest.AddQueryParam("fields","comments");

    var sbJson = new chilkat.StringBuilder();
    success = rest.FullRequestNoBodySb("GET","/drive/v3/files/0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw/comments",sbJson);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    if (rest.ResponseStatusCode !== 200) {
        console.log("Received error response code: " + rest.ResponseStatusCode);
        console.log("Response body:");
        console.log(sbJson.GetAsString());
        return;
    }

    var json = new chilkat.JsonObject();
    json.LoadSb(sbJson);

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    var i;
    var count_i;
    var kind;
    var id;
    var createdTime;
    var modifiedTime;
    var authorKind;
    var authorDisplayName;
    var authorPhotoLink;
    var authorMe;
    var htmlContent;
    var content;
    var deleted;
    var resolved;
    var j;
    var count_j;
    var action;

    i = 0;
    count_i = json.SizeOfArray("comments");
    while (i < count_i) {
        json.I = i;
        kind = json.StringOf("comments[i].kind");
        id = json.StringOf("comments[i].id");
        createdTime = json.StringOf("comments[i].createdTime");
        modifiedTime = json.StringOf("comments[i].modifiedTime");
        authorKind = json.StringOf("comments[i].author.kind");
        authorDisplayName = json.StringOf("comments[i].author.displayName");
        authorPhotoLink = json.StringOf("comments[i].author.photoLink");
        authorMe = json.BoolOf("comments[i].author.me");
        htmlContent = json.StringOf("comments[i].htmlContent");
        content = json.StringOf("comments[i].content");
        deleted = json.BoolOf("comments[i].deleted");
        resolved = json.BoolOf("comments[i].resolved");
        j = 0;
        count_j = json.SizeOfArray("comments[i].replies");
        while (j < count_j) {
            json.J = j;
            kind = json.StringOf("comments[i].replies[j].kind");
            id = json.StringOf("comments[i].replies[j].id");
            createdTime = json.StringOf("comments[i].replies[j].createdTime");
            modifiedTime = json.StringOf("comments[i].replies[j].modifiedTime");
            authorKind = json.StringOf("comments[i].replies[j].author.kind");
            authorDisplayName = json.StringOf("comments[i].replies[j].author.displayName");
            authorPhotoLink = json.StringOf("comments[i].replies[j].author.photoLink");
            authorMe = json.BoolOf("comments[i].replies[j].author.me");
            htmlContent = json.StringOf("comments[i].replies[j].htmlContent");
            content = json.StringOf("comments[i].replies[j].content");
            deleted = json.BoolOf("comments[i].replies[j].deleted");
            action = json.StringOf("comments[i].replies[j].action");
            j = j+1;
        }

        i = i+1;
    }

    console.log("Example Completed.");

}

chilkatExample();

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