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:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…