Google Drive Resumable Upload using Javascript setting permission to "Anyone with Link can View"

91 Views Asked by At

I'm implementing Google Drive file management feature into my project using the following library:

github.com/tanaikech/ResumableUploadForGoogleDrive_js

I am able to upload file onto Google Drive, however, the file uploaded is only viewable by the owner. I would like to set the file permission to be "Anyone with Link can view". Am I able to do that directly using the upload API, or must I make a separate API call later to change the permission?

Appreciate help in advance.

1

There are 1 best solutions below

7
On BEST ANSWER

I believe your goal is as follows.

In this case, how about the following modification?

From:

if (res.status == "Uploading") {
  msg =
    Math.round(
      (res.progressNumber.current / res.progressNumber.end) * 100
    ) + "%";
} else {
  msg = res.status;
}

To:

if (res.status == "Uploading") {
  msg =
    Math.round(
      (res.progressNumber.current / res.progressNumber.end) * 100
    ) + "%";
} else {
  msg = res.status;
  
  // I added the below script.
  if (msg == "Done") {
    const url = "https://www.googleapis.com/drive/v3/files/" + res.result.id + "/permissions?supportsAllDrives=true";
    fetch(url, { method: "POST", headers: { authorization: "Bearer " + resource.accessToken, "Content-Type": "application/json" }, body: JSON.stringify({ role: "reader", type: "anyone" }) })
      .then(res => res.json())
      .then(res => console.log(res));
  }

}
  • In this modification, after the file was uploaded, the permission is created using "Permissions: create".

Reference: