Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

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;
}
question from:https://stackoverflow.com/questions/65890462/box-api-by-google-apps-script-new-file-version-upload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...