【问题标题】:Google drive api: How to get the absolut link (final) as returnment From my scriptGoogle drive api:如何从我的脚本中获取绝对链接(最终)作为返回
【发布时间】:2020-12-05 00:43:31
【问题描述】:

我需要绝对的 google drive api 链接作为脚本的结果,但我不知道如何。

这是我的脚本:

我有一个 html 表单,我将媒体文件上传到谷歌驱动器并返回文件。 这是我的代码:

<form id="form">
  <input name="file" id="uploadfile" type="file">
  <input name="filename" id="filename" type="text">
  <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: form.filename.value || file.name, mimeType: file.type});
    fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
    .then(res => res.json())
    .then(e => console.log("https://drive.google.com/uc?export=download&id=" + e.fileId))
    .catch(err => console.log(err));
  }
});
</script>

这是我的服务器脚本

function doPost(e) {
  const folderId = "root";  // Folder ID which is used for putting the file, if you need.

  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);
}

一位 stackoverflow 用户描述了如何获取我需要的链接。如何将这些步骤放入脚本中?

  1. 从分享链接https://drive.google.com/file/d/your_file_id/view?usp=sharing获取文件ID

  2. 构造直接链接http://docs.google.com/uc?export=open&id=your_file_id

  3. 将直接链接粘贴到网络浏览器中,然后按回车键

  4. 在您被浏览器重定向后复制生成的 URL 注意:这将是一个更长的 URL,看起来像这样:https://doc-XX-XX-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXX/000000000000000/0000000000000000000000/*/your_file_id?e=open

使用这个最终到达 URL 是让我上传的声音文件与 Actions on Google 一起使用的唯一方法。

【问题讨论】:

标签: javascript url google-apps-script google-drive-api actions-on-google


【解决方案1】:

获取fileUrl后,使用Urlfetch获取资源。如果它被重定向,您将get status code 302Location 标头中的重定向网址。如果需要,可以使用ScriptApp.getOAuthToken() 处理授权。

const url = `https://docs.google.com/uc?export=open&id=${file.getId()}`;
const response = UrlFetchApp.fetch(url,{
  headers: {
    Authorization: `Bearer ${ScriptApp.getOAuthToken()}`
  },
  followRedirects: false
});
const statusCode = response.getStatusCode();
if (String(statusCode)[0] === "3") console.log(response.getAllHeaders()['Location'])
else console.log("No redirect")

【讨论】:

  • 感谢您的解决方案!不幸的是,我不擅长 javascript,我仍然需要学习很多东西,而且我无法将您的解决方案集成到我的服务器端脚本中。能不能插入正确,让我学习一下?
  • @MarcelDz 相反,你能告诉我你哪里有问题吗?我没有对此进行测试,您可能需要调试。我需要你学会足够的 js 来调试或者至少清楚错误信息
  • 是的,首先我尝试在我的 javascript 代码中实现它,但我注意到所有函数都仅在我的 doPost 函数的服务器端可用。现在在 doPost 函数中,我基本上不知道如何接收最终链接作为对客户端的回答
  • 1.您return 就像您现在返回responseObj 一样。 2. 能否在日志中查看所需的链接? 3.请阅读我问题中的所有链接
  • 我收到 cors 政策错误。我在 xamp 服务器上对其进行了测试,我已经创建了一个 .htaccess 文件,其标题集 Access-Control-Allow-Origin "localhost:50000",但它给了我 No 'Access-Control-Allow-Origin' header is present on请求的资源。错误
猜你喜欢
  • 1970-01-01
  • 2020-07-02
  • 2021-12-10
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多