Java 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


import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRest rest = new CkRest();
    boolean success;

    //   Provide a previously obtained OAuth2 access token.
    CkOAuth2 oauth2 = new CkOAuth2();
    oauth2.put_AccessToken("OAUTH2_ACCESS_TOKEN");
    rest.SetAuthOAuth2(oauth2);

    success = rest.Connect("www.googleapis.com",443,true,true);
    if (success != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

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

    CkStringBuilder sbJson = new CkStringBuilder();
    success = rest.FullRequestNoBodySb("GET","/drive/v3/files/0B5drHSd5ZHwgc3RhcnRlcl9maWxlX2Rhc2hlclYw/comments",sbJson);
    if (success != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    if (rest.get_ResponseStatusCode() != 200) {
        System.out.println("Received error response code: " + rest.get_ResponseStatusCode());
        System.out.println("Response body:");
        System.out.println(sbJson.getAsString());
        return;
        }

    CkJsonObject json = new CkJsonObject();
    json.LoadSb(sbJson);

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

    i = 0;
    count_i = json.SizeOfArray("comments");
    while (i < count_i) {
        json.put_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.put_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;
        }

    System.out.println("Example Completed.");
  }
}

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