I want to use Google Apps Script to add a Drive Label (that already exists) to a file (that already exists).
I think I'm having a hard time getting the Request Body correct. If I run my fileID and LabelID through the "try this method" helper here it works, but when I try to replicate this in Google Apps Script, I'm not getting an error, but the label is not being applied and the Response Body comes back blank.
var fileId = 'MY FILE ID';
var test3 =Drive.newModifyLabelsRequest({
labelModifications:[{
labelId: 'MY LABEL ID',
}]
});
var output = Drive.Files.modifyLabels(test3,fileId);
Logger.log(output.modifiedLabels);
I originally had it written as.
Drive.Files.modifyLabels ('FileID',Request Body);
but got errors about it not parsing the Json correctly and noticed that the IDE reference put the 'Resource' before the 'fileID', so I swapped them.
Couldn't find any other posts here that relate to this.
I figured this out on my own.
There is a method that isn't deeply referenced that doesn't show up in the IDE's helper of Google Apps Script. The missing piece is .setLabelModifications(). It is similar to the Java referenced here
For my need the below is the correct syntax of the code.
var fileId = 'YOUR FILE ID';
var AddLabel =Drive.newModifyLabelsRequest()
.setLabelModifications(
Drive.newLabelModification()
.setLabelId("YOUR LABEL ID")
);
var output = Drive.Files.modifyLabels(AddLabel,fileId);
Logger.log(output.modifiedLabels);