Currently working on a google sheet and implementing a script that will upload a file from Drive to BOX . I got the authentication down (I am able to get the folder list through the script connecting to the API) but when I get to the uploading part, I am getting an empty request and the file is not getting uploaded to the target folder
My script below:
function uploadFile() {
var boundary = "test";
var blob = DriveApp.getFileById('<I got my file ID here>').getBlob();
var service = getService();
var attributes = "{\"name\":\"test document.pdf\", \"parent\":{\"id\":\"<I got my folder ID here>\"}}";
var requestBody = Utilities.newBlob(
"--"+boundary+"\r\n"
+ "Content-Disposition: form-data; name=\"attributes\"\r\n\r\n"
+ attributes+"\r\n"+"--"+boundary+"\r\n"
+ "Content-Disposition: form-data; name=\"file\"; filename=\""+blob.getName()+"\"\r\n"
+ "Content-Type: " + blob.getContentType()+"\r\n\r\n").getBytes()
.concat(blob.getBytes())
.concat(Utilities.newBlob("\r\n--"+boundary+"--\r\n").getBytes());
var options = {
method: "post",
contentType: "multipart/form-data; boundary="+boundary,
payload: requestBody,
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer ' + service.getAccessToken()}
};
var request = UrlFetchApp.fetch("https://upload.box.com/api/2.0/files/content", options);
Logger.log(request.getContentText()); // empty response
}
Thanks
When I check the official document of "Upload file" of the BOX API, I found the following curl sample. Ref
$ curl -i -X POST "https://upload.box.com/api/2.0/files/content" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-H "Content-Type: multipart/form-data" \
-F attributes="{"name":"Contract.pdf", "parent":{"id":"11446498"}}" \
-F file=@<FILE_NAME>
When the request of multipart/form-data
is run, it seems that at Google Apps Script, the content type is automatically created including the boundary.
When above curl command is converted to Google Apps Script, it becomes as follows.
function uploadFile() {
var blob = DriveApp.getFileById('<I got my file ID here>').getBlob();
var service = getService();
var attributes = {"name":"test document.pdf", "parent":{"id":"<I got my folder ID here>"}};
var requestBody = {attributes: Utilities.newBlob(JSON.stringify(attributes), "application/json"), file: blob};
var options = {
method: "post",
payload: requestBody,
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer ' + service.getAccessToken()}
};
var request = UrlFetchApp.fetch("https://upload.box.com/api/2.0/files/content", options);
Logger.log(request.getContentText());
}
service.getAccessToken()
can be used for uploading the file using the API. Please be careful this.