Search code examples
filegoogle-apps-scriptversionbox-api

BOX API by Google Apps Script - new file version upload


I've already asked the GAS community but I was advised to continue asking here... So far I'm able to connect to BOX and get a list of files and I can download a file from BOX as well. The whole idea is to download a file using BOX API, edit it and upload it back as a new file version using the BOX API. I'm unable to make the last part working as it gives me error code 400. Here is the function.

function uploadNewFileVersion() {
  //767694355309 testing
  var boxFileId="767694355309";
  var newVerFile = DriveApp.getFileById("1sK-jcaJoD0WaAcixKtlHA85pf6t8M61v").getBlob();
  var confirmAuthorization = getBoxService_().getAccessToken();

  //var parent = { "id": "0" };

//"name": "apiNewVersion.xlsx",
//"parent": parent,  
  var payload = {
    "file": newVerFile
  }
  var headers = {
    'Authorization': 'Bearer ' + confirmAuthorization
  }
  var options = {
    "method": "post",
    "muteHttpExceptions": true,
    "contentType": "multipart/form-data",
    "headers": headers,
    "payload": payload
  }

  var apiHtml = "https://upload.box.com/api/2.0/files/"+boxFileId+"/content/";
  var response = UrlFetchApp.fetch(apiHtml, options);
  Logger.log(response.getResponseCode());
  var a = 1;
}

The boxFileId is the file on the box. The newVerFile is the one downloaded from Box and updated. I need to make it as a new version of the Box file.

Could you please advise? Thank you! PEtr

I think parent and name is optional so I commented it out. If I don't getBlob, then it returns 415 istead.


Solution

  • I believe your goal and situation as follows.

    • You want to upload a file of Google Drive using Box API with Google Apps Script.
    • From your question, I cannot find the official document of the method of API that you want to use. But, from the endpoint https://upload.box.com/api/2.0/files/"+boxFileId+"/content/ in your script, I guessed that you wanted to use "Upload file version".
    • Values of your access token and file ID are valid for using the API.

    If my understanding of your question is correct, how about the following modification?

    Modification points:

    • When I saw the official document of "Upload file version", I confirmed the following sample curl. In this case, it is considered that when the following curl command is converted to Google Apps Script, the request might work.

        $ curl -i -X POST "https://upload.box.com/api/2.0/files/12345/content" \
             -H "Authorization: Bearer <ACCESS_TOKEN>" \
             -H "Content-Type: multipart/form-data" \
             -F attributes="{"name":"Contract.pdf", "parent":{"id":"11446498"}}" \
             -F file=@<FILE_NAME>
      
      • From the curl command, it is found that attributes and file are sent as form and files.
      • And, I thought that attributes="{"name":"Contract.pdf", "parent":{"id":"11446498"}}" might should be attributes="{\"name\":\"Contract.pdf\", \"parent\":{\"id\":\"11446498\"}}".
    • When I saw your current script, it seems that multipart/form-data is used for contentType. In this case, boundary in the request body is required to be included. Fortunately, at UrlFetchApp, in the case of multipart/form-data, when contentType is not used, the content type is automatically included in the request header. I think that in your case, this can be used.

    • In your script, attributes="{"name":"Contract.pdf", "parent":{"id":"11446498"}}" is not included. But I thought that you might use it in the future script. So in this answer, this is also included.

    When above points are reflected and the sample curl command on the official document is converted to Google Apps Script, the script becomes as follows.

    Sample script:

    Please copy and paste the following script to the script editor and set the variables, and run the function of myFunction. By this, the request same with the sample curl is requested with Google Apps Script.

    function myFunction() {
      const accessToken = "###"; // Please set your access token.
      const fileId = "###"; // Please set your fileId.
      const fileBlob = DriveApp.getFileById("1sK-jcaJoD0WaAcixKtlHA85pf6t8M61v").getBlob();
      const metadata = {name: "Contract.pdf", parent: {id: "11446498"}};  // Please set your file metadata.
    
      const params = {
        method: "post",
        headers: {Authorization: `Bearer ${accessToken}`},
        payload: {
          attributes: JSON.stringify(metadata),
          file: fileBlob,
        },
        muteHttpExceptions: true,
      };
      const url = `https://upload.box.com/api/2.0/files/${fileId}/content`;
      const res = UrlFetchApp.fetch(url, params);
      console.log(res.getContentText());
    }
    
    • I could confirm that above sample script is the same request with above sample curl.
    • If you don't want to use the file metadata, please remove the line of attributes: JSON.stringify(metadata), from payload.

    Note:

    • In this case, the maximum data size ("URL Fetch POST size") of UrlFetchApp is 50 MB. Please be careful this. Ref
    • About the limitation of file upload of Box API, please check https://developer.box.com/guides/uploads/.
    • If your access token and file ID are invalid, I think that an error occurs. So please be careful this.

    References: