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
- Comments: https://developers.google.com/drive/api/v3/reference/comments/create
- Anchored
comments.create
: https://developers.google.com/drive/api/guides/manage-comments#anchor - Anchor regions: https://developers.google.com/drive/api/guides/manage-comments#define
Google Apps Script