无法在Google应用程序脚本中提供下载链接 [英] Unable to serve download links in google apps script

查看:119
本文介绍了无法在Google应用程序脚本中提供下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我找到了一个解决方案。这并不一定涉及每一个案件,所以我会在短时间内将问题留在这里,以免有人能够更多地启发我。我通过更改网址的格式来解决此问题:Google云端硬盘允许使用此格式下载文件: https://docs.google.com/uc?export=download&id=FILE_ID



所以我不知道这是否是其他URL的问题;实际上也不是确切的原因.getDownloadUrl()不工作......也许有人可以解释。但现在,这似乎在我可以测试的浏览器中工作......






我有一个简单的WebApp脚本,它我通过添加Apps脚本小工具在Google网站上运行。该小工具的运行方式与以下表单示例完全一致:

https://developers.google.com/apps-script/guides/html/communication#forms



小工具旨在执行以下操作:加载页面时,将返回表单,并且用户必须输入许可证密钥才能获得下载产品的链接。我的代码服务表单确定,并获取表单提交确定;然后验证密钥,如果有效,则发回一个链接进行下载。一切工作正常;问题是无论我尝试返回下载链接,caja iframe包装都阻止点击链接实际下载文件。



我的首选要返回的网址实际上是通过Drive API访问的:下载文件位于Google云端硬盘上,并且可以下载链接,如下所示:



DriveApp.getFileById(downloadFileId ).getDownloadUrl()



但是,当为WebApp小工具生成的caja iframe中单击返回的链接时,什么都不会发生。我已经尝试了一些指向该驱动器上该文件的其他URL格式,但没有任何内容适用于下载。



这可能吗?

解决方案

.getDownloadUrl() 方法返回一个可用于下载文件的临时 URL。此网址仅在很短的时间内有效,之后过期并且不会再返回该文件 - 这可能是您的网络应用中的链接无法使用的原因。无法准确记住网址的有效期限,但我认为它可能短至5分钟。



永久下载URL存储在另一个文件属性中: webContentLink 。不过,此功能尚未通过Google Apps云端硬盘服务提供 - 您必须使用高级云端硬盘服务来访问它。 您可以在脚本中的高级Google服务下启用Drive API 。启用后,您可以像这样使用它:

  var file = Drive.Files.get(FILE_ID_HERE); 
var dlUrl = file.webContentLink;

这将返回与您在更新中找到并发布的链接相同的链接。使用Drive API获取链接而不是对其进行硬编码的优势在于,如果Google更改了该网址的格式,则使用Drive API获取链接的代码将继续有效,而硬编码链接不会。



Full Drive Web API参考(Advanced Drive Service使用的参考)位于 https://developers.google.com/drive/v2/reference/


UPDATE: I have found a solution. This doesn't necessarily address every case, so I will leave the question open for a short time in case someone can enlighten me more. I solved it by changing the format of the url: Google Drive allows this format for downloading files:

https://docs.google.com/uc?export=download&id=FILE_ID

So I don't know if this is a problem for other URL's; nor actually exactly why the .getDownloadUrl() doesn't work ... maybe someone can explain. But for now this seems to work in the browsers that I can test ...


I have a simple WebApp script which I run on a Google Site by adding the Apps Script gadget. The gadget runs exactly as the Forms example on:

https://developers.google.com/apps-script/guides/html/communication#forms

The gadget is designed to do the following: when the page is loaded, a form is returned, and the user must enter a license key to get a link to download a product. My code serves the form OK, and gets the form submit OK; and it then validates the key, and if valid, sends back a link to download. All that works fine; and the problem is that no matter what I try to return for the download link, the caja iframe wrapper is preventing the click on the link from actually downloading the file.

My preferred URL to return is in fact via the Drive API: the download file is on the Google Drive, and I get the download link like so:

DriveApp.getFileById(downloadFileId).getDownloadUrl()

But when the returned link is clicked inside that caja iframe generated for the WebApp gadget, nothing happens. I have tried a few other URL formats pointing to that file on the Drive, but nothing is working for a download.

Is this possible?

解决方案

.getDownloadUrl() method returns a temporary URL that can be used to download the file. This URL is valid only for a short period of time, after which it expires and does not return the file any more - that is probably why the links in your web app do not work. Can't remember exactly how long the URL is valid for, but I think it could be as short as 5 minutes.

Permanent download URL is stored in another file property: webContentLink. However, this property is not (yet) available through Google Apps Drive Service - you must use Advanced Drive Service to access it. You can enable Drive API under Advanced Google services in your script. After it is enabled, you can use it like so:

var file = Drive.Files.get(FILE_ID_HERE);
var dlUrl = file.webContentLink;

This will return the link just like the one you found and posted in your update. An advantage of using the Drive API to get the link, instead of hard-coding it, is that if Google ever changes the format of that URL, your code using Drive API to get the link will continue to work, while hard-coded links will not.

Full Drive Web API reference (what Advanced Drive Service uses) is at https://developers.google.com/drive/v2/reference/.

这篇关于无法在Google应用程序脚本中提供下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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