由匿名用户将文件上传到Google云端硬盘

时间:2017-05-20 10:57:38

标签: google-apps-script google-drive-api

我使用Google Apps脚本。

Code.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function uploadFile(form) {
  var folderId = "folder_id";

  try {

    var folder = DriveApp.getFolderById(folderId);

    var blob = form.picToLoad;
    var file = folder.createFile(blob);

    return "File uploaded successfully " + file.getUrl();

  } catch (error) {
    Logger.log(error);

    return error.toString();
  }
}

form.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1 id="main-heading">Main Heading</h1>
    <br/>
    <div id="formDiv">
      <form id="myForm">
        <input name="picToLoad" type="file" /><br/>
        <input type="button" value="Submit" onclick="picUploadJs(this.parentNode)" />
      </form>
    </div>
  <div id="status" style="display: none">
  Uploading. Please wait...
</div>

</body>
<script>
  function picUploadJs(frmData) {
    document.getElementById('status').style.display = 'inline';
    google.script.run
      .withSuccessHandler(updateOutput)
      .uploadFile(frmData)
  };

  function updateOutput() {
    var outputDiv = document.getElementById('status');
    outputDiv.innerHTML = "The File was UPLOADED!";
  }
</script>
</html>

当我在我的域中进行身份验证(我使用G Suite)时,一切正常。

但是,如果我作为其他用户(例如普通Gmail用户)登录Google或根本没有登录,我仍然可以访问该页面,但脚本无法正常执行以下操作控制台中出错:

错误

google.script.run.withSuccessHandler(...).processForm is not a function
  at picUploadJs (VM84 userCodeAppPanel:9)
  at HTMLInputElement.onclick (VM104 userCodeAppPanel:1)

在Apps脚本级别的日志中不会显示其他日志。

我已将脚本部署为:

  

执行应用程序:我(并授权)

     

谁可以访问该应用:任何人,甚至匿名

所以,我认为一切都应该正常工作,任何人都应该能够将文件上传到我的驱动器。不幸的是,事实并非如此。

同样,只有当我从域外访问脚本时才会发生这种情况。

任何人都可以看到错误吗?

1 个答案:

答案 0 :(得分:1)

正如Zig Mandel的评论中所建议的,制作脚本并运行它的副本解决了这个问题。这重新启动了授权过程,因此可能会错误地初始化权限。