Search code examples
dartgoogle-docs-api

Issue adding comments on Google Doc (Done all the work required in retrieving comments from Google Doc)


Could you please help me with an issue I am having adding comments on Google Doc? I have done all the work required in retrieving comments from Google Drive. This is how I retrieve comments from Google Doc:

String fileId ='1up_-hN_z6Fv4NuPUDZioIzFgk42o4j6n8'; 

String commentId = "AAAAp8ow"; 

googleUser = await _googleUserSignIn.signIn(); 

final authHeaders = await googleUser!.authHeaders; 

final client = GoogleHttpClient(authHeaders); 

print(' passed 1 '); 

final marcoDrive = drive.DriveApi(client); 

var  result = await marcoDrive.comments.get(fileId, commentId, $fields: 'content'); 

The screenshot below shows the debugging results:

enter image description here

As you can see on the screenshot above, the results variable contains a comment data fetch from Google Doc. The screenshot below shows Google Doc and the orange circle is the comment: enter image description here

My issue is creating a comment on Google Doc. I find the Comment request parameter, highlighted below using an orange arrow, problematic as I am struggling to create a comment in Google Doc.

var result2 = await marcoDrive.comments.create(request, fileId, $fields: 'content: data is data');

enter image description here

Could you please help me to understand what the Comment request parameter means and how to use it? I would also appreciate it if you could help me understand how to anchor comments.

The programming language I'm using is Dart. Here is a link that I used to help me understand how to manage Google Drive Doc comments: (https://developers.google.com/drive/api/v3/manage-comments).


Solution

  • You have to supply a Comment resource as your request body, as you can see at the official docs: Comments.create.

    Here are the fields corresponding to the Comment resource. The only required field is content.

    The JSON representation would be something like:

    {
      "content": "YOUR COMMENT"
    }
    

    In Dart, it would probably be something along the following lines:

    Comment request = Comment();
    request.content = "YOUR COMMENT";
    var result = await marcoDrive.comments.create(request, fileId, $fields);
    

    Library reference: