4

Why does Google Drive API v3 Comments throw a writableFieldRequired error even when writable properties (comment and anchor) are included?

Note: While it was not possible to add anchored comments in Drive API v2, the docs for v3 show that this is already a possibility. Since the Drive service in GAS is still in v2 (as of March 2023), is the reason why the code below uses UrlFetchApp to call Drive API v3 externally.

Code:

function testComment() {

  var doc = DocumentApp.getActiveDocument();
  var token = ScriptApp.getOAuthToken();

  var docId = doc.getId();
  var urlDriveCommentsV3 = 'https://www.googleapis.com/drive/v3/files/' + docId + '/comments?fields=id';

  var anchor = {
    'r': 'head',
    'a': [
      {
        'txt': {
          'o': 1,
          'l': 6
        }
      }
    ]
  };

  var commentv3 = {
    'content': 'This is a test comment 01',
    'anchor': anchor
  };

  var options = {
    'method': 'post',
    'headers': {
      'Authorization': 'Bearer ' +  token,
      'Accept': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    'payload': commentv3,
    'muteHttpExceptions': true
  };

  var result = UrlFetchApp.fetch(urlDriveCommentsV3, options);

  Logger.log(result);
};

Error log:

{
  "error": {
    "code": 400,
    "message": "At least one writable field must be specified in the resource body.",
    "errors": [
      {
        "message": "At least one writable field must be specified in the resource body.",
        "domain": "global",
        "reason": "writableFieldRequired"
      }
    ]
  }
}

References:

Drive API v3

Google Apps Script

Majal
  • 1,635
  • 20
  • 31
  • Does this answer your question? [How do i add a comment to a particular google slide using app script](https://stackoverflow.com/questions/67014367/how-do-i-add-a-comment-to-a-particular-google-slide-using-app-script) – Giselle Valladares Mar 24 '23 at 18:51
  • Also this issue tracker https://issuetracker.google.com/36763384 – Giselle Valladares Mar 24 '23 at 19:57
  • Thank you for your response Giselle, I have checked those references. On the other hand they all seem to refer to Drive API v2. Docs for v3 show that we may now call `comments.create` with anchors: https://developers.google.com/drive/api/guides/manage-comments#anchor – Majal Mar 24 '23 at 23:35

0 Answers0