Trying to alter 'createdTime' in google docs with Google Apps Script. Getting Error 403 ..."field not writable". Any workaround?

2020-02-14 google-drive-api

Full error message:

Request failed for https://www.googleapis.com returned code 403. Truncated server response: { "error": { "errors": [ { "domain": "global", "reason": "fieldNotWritable", "message": "The resource body includes fields which ... (use muteHttpExceptions option to examine full response). (line 13, file "Code")

The code below worked with 'viewedByMeTime' & 'modifiedTime'

function myFunction() {
  var newModifiedTime = "2019-01-01T00:00:00.000Z"; 
  var fileId = "1LaKH-wsjgGrCyG6zpbyzsBgsgTah2jQRIIDvDKaaRio";  
  var url = "https://www.googleapis.com/drive/v3/files/" + fileId;

var params = {
    method: "patch",
    headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},//Last time opend by you
    payload: JSON.stringify({createdTime: newModifiedTime}),
    contentType: "application/json",
  };
  UrlFetchApp.fetch(url, params);

Answers

You appear to be trying to do a file.update

"fieldNotWritable"

means exactly that not all of the fields can be writen to. which means you cant update every fields. and create time is one of the fields that you cant update. if you check the documentation under request body you will see the fields you are allowed to update.

Related