Google Drive API:如何从我的脚本中获取绝对链接(最终)作为退货 [英] Google drive api: How to get the absolut link (final) as returnment From my script

查看:178
本文介绍了Google Drive API:如何从我的脚本中获取绝对链接(最终)作为退货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于脚本的原因,我需要绝对的google drive api链接,但我不知道如何.

I need the absolute google drive api link as result from my script but I don’t know how.

这是我的脚本:

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

I have a html form and Im uploading a media file to google drive and return the file. This is my code:

<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用户描述了如何获取我需要的链接.如何在脚本中放置此步骤?

One stackoverflow User described how to get the link I need. How to put this steps in the script?

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

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

将直接链接粘贴到Web浏览器中,然后按Enter键

Paste the direct link into a web browser and hit enter

通过浏览器重定向后,复制结果URL注意:这将是一个更长的URL,看起来像这样:

Copy the resulting URL after you have been redirected by your browser Note: This will be a much longer URL that looks something like this: https://doc-XX-XX-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXX/000000000000000/0000000000000000000000/*/your_file_id?e=open

使用此最终到达网址是我可以使上传的声音文件与Google上的Actions一起使用的唯一方法.

Using this final URL is the only way I could get my uploaded sound files to work with Actions on Google.

推荐答案

获取fileUrl后,使用获取状态代码 302 和重定向的网址在 Location 标头中.可以使用 ScriptApp.getOAuthToken() 处理授权,如果需要的话.

After getting the fileUrl, use Urlfetch to fetch the resource. If it is redirected, you'll get status code 302 with the redirected url in Location header. Authorization can be handled with ScriptApp.getOAuthToken(), if needed.

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")

这篇关于Google Drive API:如何从我的脚本中获取绝对链接(最终)作为退货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆