问题描述
我的桌面应用程序用java编写,尝试从Google Drive下载公共文件。正如我发现的那样,它可以通过使用文件 webContentLink
来实现(它可以在没有用户授权的情况下下载公共文件)。$ b $因此,下面的代码适用于小文件:
字符串webContentLink = aFile.getWebContentLink();
InputStream in =新的URL(webContentLink).openStream();
但它不适用于大文件,因为在这种情况下文件不能直接下载通过 webContentLink
无谷歌病毒扫描警告的用户确认。查看示例:
工具或仅制作工具来创建/生成直接访问链接您自己的链接:
https://docs.google.com/uc?export=download&id=<您的文件ID>
您将收到一个基于Cookie的验证码并提示Google无法扫描此文件提示,这对于诸如 wget 或Vagrantfile configs。
它生成的代码是一个附加GET查询变量的简单代码。 & confirm = ###
到字符串,但它是按用户的具体情况,所以不是就像你可以复制/粘贴查询变量给其他人一样。
但是如果你使用上面的网页托管方法,你可以绕过那个提示。
我希望有帮助!
My desktop application, written in java, tries to download public files from Google Drive. As i found out, it can be implemented by using file's webContentLink
(it's for ability to download public files without user authorization).
So, the code below works with small files:
String webContentLink = aFile.getWebContentLink();
InputStream in = new URL(webContentLink).openStream();
But it doesn't work on big files, because in this case file can't be downloaded directly via webContentLink
without user confirmation with google virus scan warning. See an example: web content link.
So my question is how to get content of a public file from Google Drive without user authorization?
Update December 8th, 2015According to Google Support using the
googledrive.com/host/ID
method will be turned off on Aug 31st, 2016.
I just ran into this issue.
The trick is to treat your Google Drive folder like a web host.
Update April 1st, 2015
Google Drive has changed and there's a simple way to direct link to your drive. I left my previous answers below for reference but to here's an updated answer.
- Create a Public folder in Google Drive.
- Share this drive publicly.
- Get your Folder UUID from the address bar when you're in that folder
- Put that UUID in this URL
https://googledrive.com/host/<folder UUID>/
- Add the file name to where your file is located.
https://googledrive.com/host/<folder UUID>/<file name>
All you have to do is simple get the host URL for a publicly shared drive folder. To do this, you can upload a plain HTML file and preview it in Google Drive to find your host URL.
Here are the steps:
- Create a folder in Google Drive.
- Share this drive publicly.
- Upload a simple HTML file. Add any additional files (subfolders ok)
- Open and "preview" the HTML file in Google Drive
- Get the URL address for this folder
- Create a direct link URL from your URL folder base
- This URL should allow direct downloads of your large files.
[edit]
I forgot to add. If you use subfolders to organize your files, you simple use the folder name as you would expect in a URL hierarchy.
https://googledrive.com/host/<your public folders id string>/images/my-image.png
What I was looking to do
I created a custom Debian image with Virtual Box for Vagrant. I wanted to share this ".box" file with colleagues so they could put the direct link into their Vagrantfile.
In the end, I needed a direct link to the actual file.
Google Drive problem
If you set the file permissions to be publicly available and create/generate a direct access link by using something like the gdocs2direct tool or just crafting the link yourself:
https://docs.google.com/uc?export=download&id=<your file id>
You will get a cookie based verification code and prompt "Google could not scan this file" prompt, which won't work for things such as wget or Vagrantfile configs.
The code that it generates is a simple code that appends GET query variable ...&confirm=###
to the string, but it's per user specific, so it's not like you can copy/paste that query variable for others.
But if you use the above "Web page hosting" method, you can get around that prompt.
I hope that helps!
这篇关于使用Google Drive API直接从Google云端硬盘下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!