Google 驱动器 api:如何从我的脚本中获取绝对值 link(最终)作为返回值
Google drive api: How to get the absolut link (final) as returnment From my script
我需要绝对 google 驱动器 api link 作为脚本的结果,但我不知道如何。
这是我的脚本:
我有一个 html 表格,我正在将媒体文件上传到 google 驱动器和 return 文件。
这是我的代码:
<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);
}
一位 Whosebug 用户描述了如何获得我需要的 link。如何将这些步骤放入脚本中?
从分享中获取文件ID link https://drive.google.com/file/d/your_file_id/view?usp=sharing
构造直接linkhttp://docs.google.com/uc?export=open&id=your_file_id
将直接 link 粘贴到网络浏览器中,然后按回车键
在浏览器重定向后复制结果 URL 注意:这将是一个更长的 URL,看起来像这样:https://doc-XX-XX-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXX/000000000000000/0000000000000000000000/*/your_file_id?e=open
使用这个最终 URL 是让我上传的声音文件与 Google 上的 Actions 一起工作的唯一方法。
获取 fileUrl 后,如果需要,请使用 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()
。
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 驱动器 api link 作为脚本的结果,但我不知道如何。
这是我的脚本:
我有一个 html 表格,我正在将媒体文件上传到 google 驱动器和 return 文件。 这是我的代码:
<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);
}
一位 Whosebug 用户描述了如何获得我需要的 link。如何将这些步骤放入脚本中?
从分享中获取文件ID link https://drive.google.com/file/d/your_file_id/view?usp=sharing
构造直接linkhttp://docs.google.com/uc?export=open&id=your_file_id
将直接 link 粘贴到网络浏览器中,然后按回车键
在浏览器重定向后复制结果 URL 注意:这将是一个更长的 URL,看起来像这样:https://doc-XX-XX-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXX/000000000000000/0000000000000000000000/*/your_file_id?e=open
使用这个最终 URL 是让我上传的声音文件与 Google 上的 Actions 一起工作的唯一方法。
获取 fileUrl 后,如果需要,请使用 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()
。
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")