This is a sample script for uploading a file to Google Drive from the external HTML without the authorization. In this case, the client side can be used at the outside of Google. And as the server side, the Web Apps created by Google Apps Script is used.
Please do the following flow.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
Please copy and paste the following script (Google Apps Script) to the script editor. This script is for the Web Apps.
Please set the folder ID that you want to put the file.
function doPost(e) {
const folderId = "###"; // Folder ID which is used for putting the file.
const blob = Utilities.newBlob(
JSON.parse(e.postData.contents),
e.parameter.mimeType,
e.parameter.filename
);
const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
const responseObj = {
filename: file.getName(),
fileId: file.getId(),
fileUrl: file.getUrl(),
};
return ContentService.createTextOutput(
JSON.stringify(responseObj)
).setMimeType(ContentService.MimeType.JSON);
}
- When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.
- On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
- Select "Me" for "Execute the app as:".
- By this, the script is run as the owner.
- Select "Anyone, even anonymous" for "Who has access to the app:".
- Click "Deploy" button as new "Project version".
- Automatically open a dialog box of "Authorization required".
- Click "Review Permissions".
- Select own account.
- Click "Advanced" at "This app isn't verified".
- Click "Go to ### project name ###(unsafe)"
- Click "Allow" button.
- Click "OK".
- Copy the URL of Web Apps. It's like
https://script.google.com/macros/s/###/exec
.- When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
Please set the URL of your Web Apps to the following script.
<form id="form">
<input name="file" id="uploadfile" type="file" />
<input id="submit" type="submit" />
</form>
<script>
const form = document.getElementById("form");
form.addEventListener("submit", (e) => {
e.preventDefault();
const file = form.file.files[0];
const fr = new FileReader();
fr.readAsArrayBuffer(file);
fr.onload = (f) => {
const url = "https://script.google.com/macros/s/###/exec"; // Please set the URL of Web Apps.
const qs = new URLSearchParams({
filename: file.name,
mimeType: file.type,
});
fetch(`${url}?${qs}`, {
method: "POST",
body: JSON.stringify([...new Int8Array(f.target.result)]),
})
.then((res) => res.json())
.then(console.log)
.catch(console.log);
};
});
</script>
- At the client side, when you selected a file from your local PC and push the button, the file is uploaded to your Google Drive by retrieving the data at the Web Apps (server side).
In this case, the maximum file size is 50 MB. Because in the current stage, the maximum blob size is 50 MB at Google Apps Script.
Hello,
I'm trying to use this script as a reference to upload some JSON data to a Google Drive folder via a fetch/AJAX POST request from a web/mobile app I'm developing.
I have copied the following code from Step 2 into my Google Apps Script editor, doing some slight modifications to fit the needs of my app:
I have also deployed the Apps Script project following the settings given in Step 3:
Now I am testing the deployed Apps Script using Postman, setting the appropriate
filename
,mimeType
, and file contents (encoded in base64) on the request parameters to simulate a fetch/AJAX POST request on my deployed Apps Script, and I keep getting a401 Unauthorized
response when I try to send the request:For some reason, that
"Sorry, unable to open the file at this time"
message appears whenever I try to send a POST request without logging in to my Google account (which currently owns the Google Drive folder I want to upload the files on). When I send the same POST request with my Google account logged in, the request is successful and I can see the file being uploaded to my Google Drive folder.I think the current issue with my Apps Script is that it checks if my Google account is logged in, even though I have deployed the Apps Script to be usable by anyone (ideally without having to log in to a Google account as well). Do I need to set another deployment option to let other users upload to my Google Drive folder without having to log in?
I've also tried to search around for some possible solutions for this, and one interesting result suggested I use the OAuth 2.0 Playground to generate an OAuth Bearer token to authorize(?) my POST request before I can successfully upload the file:
Setting the Bearer token does get me my desired result, which is to let users upload some JSON data to my Google Drive folder without having to log in.
This current approach seems fine and all, but the problem I see with it is that I have to automatically refresh the OAuth token after some time and modify my POST request using the newly generated OAuth token. Is there a way to do this without having to rewrite my code just to change the OAuth token?