简体   繁体   English

使用 Google Drive API 从 Google Drive 直接下载

[英]Direct download from Google Drive using Google Drive API

My desktop application, written in java, tries to download public files from Google Drive.我用 Java 编写的桌面应用程序尝试从 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).正如我发现的,它可以通过使用文件的webContentLink来实现(它可以在没有用户授权的情况下下载公共文件)。

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.但它不适用于大文件,因为在这种情况下, webContentLink没有用户确认和谷歌病毒扫描警告,则无法通过webContentLink直接下载文件。 See an example: web content link .查看示例: 网页内容链接

So my question is how to get content of a public file from Google Drive without user authorization?所以我的问题是如何在未经用户授权的情况下从 Google Drive 获取公共文件的内容?

Update December 8th, 2015 According to Google Support using the 2015 年 12 月 8 日更新根据Google 支持使用

googledrive.com/host/ID

method will be turned off on Aug 31st, 2016.方法将于 2016 年 8 月 31 日关闭。


I just ran into this issue.我刚刚遇到了这个问题。

The trick is to treat your Google Drive folder like a web host.诀窍是将您的 Google Drive 文件夹视为网络主机。

Update April 1st, 2015 2015 年 4 月 1 日更新

Google Drive has changed and there's a simple way to direct link to your drive. Google 云端硬盘发生了变化,并且提供了一种直接链接到您的云端硬盘的简单方法。 I left my previous answers below for reference but to here's an updated answer.我在下面留下了我以前的答案以供参考,但这里有一个更新的答案。

  1. Create a Public folder in Google Drive.在 Google Drive 中创建一个公共文件夹。

  2. Share this drive publicly.公开共享此驱动器。

    在此处输入图片说明

  3. Get your Folder UUID from the address bar when you're in that folder当您在该文件夹中时,从地址栏中获取您的文件夹 UUID

    在此处输入图片说明
  4. Put that UUID in this URL将该 UUID 放在此 URL 中

    https://googledrive.com/host/<folder UUID>/
  5. Add the file name to where your file is located.将文件名添加到文件所在的位置。

    https://googledrive.com/host/<folder UUID>/<file name>

Which is intended functionality by Google 这是 Google 的预期功能
new Google Drive Link .新的 Google 云端硬盘链接

All you have to do is simple get the host URL for a publicly shared drive folder.您所要做的就是获取公共共享驱动器文件夹的主机URL。 To do this, you can upload a plain HTML file and preview it in Google Drive to find your host URL.为此,您可以上传纯 HTML 文件并在 Google 云端硬盘中预览以查找您的主机 URL。

Here are the steps:以下是步骤:

  1. Create a folder in Google Drive.在 Google Drive 中创建一个文件夹。

  2. Share this drive publicly.公开共享此驱动器。

    在此处输入图片说明

  3. Upload a simple HTML file.上传一个简单的 HTML 文件。 Add any additional files (subfolders ok)添加任何其他文件(子文件夹可以)

    在此处输入图片说明

  4. Open and "preview" the HTML file in Google Drive在 Google Drive 中打开并“预览”HTML 文件

    在此处输入图片说明

  5. Get the URL address for this folder获取此文件夹的 URL 地址

    在此处输入图片说明

  6. Create a direct link URL from your URL folder base从您的 URL 文件夹库创建直接链接 URL

    在此处输入图片说明

  7. This URL should allow direct downloads of your large files.此 URL 应允许直接下载您的大文件。

[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.如果您使用子文件夹来组织您的文件,您可以简单地使用文件夹名称,就像您在 URL 层次结构中所期望的那样。

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.我使用 Virtual Box for Vagrant 创建了一个自定义 Debian 映像。 I wanted to share this ".box" file with colleagues so they could put the direct link into their Vagrantfile.我想与同事分享这个“.box”文件,这样他们就可以将直接链接放入他们的 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:如果您将文件权限设置为公开可用并通过使用类似gdocs2direct工具或自己制作链接来创建/生成直接访问链接:

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.您将获得基于 cookie 的验证码并提示“Google 无法扫描此文件”提示,这不适用于wget或 Vagrantfile 配置等内容。

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.它生成的代码是将 GET 查询变量...&confirm=###附加到字符串的简单代码,但它是针对每个用户的,因此您不能为其他人复制/粘贴该查询变量。

But if you use the above "Web page hosting" method, you can get around that prompt.但是,如果您使用上述“网页托管”方法,则可以绕过该提示。

I hope that helps!我希望这有帮助!

If you face the "This file cannot be checked for viruses" intermezzo page, the download is not that easy.如果您遇到“无法检查此文件是否有病毒”的插曲页面,下载就不是那么容易了。

You essentially need to first download the normal download link, which however redirects you to the "Download anyway" page.您基本上需要先下载正常的下载链接,但该链接会将您重定向到“仍然下载”页面。 You need to store cookies from this first request, find out the link pointed to by the "Download anyway" button, and then use this link to download the file, but reusing the cookies you got from the first request.您需要存储来自第一个请求的 cookie,找出“仍然下载”按钮指向的链接,然后使用此链接下载文件,但重用您从第一个请求中获得的 cookie。

Here's a bash variant of the download process using CURL:这是使用 CURL 的下载过程的 bash 变体:

curl -c /tmp/cookies "https://drive.google.com/uc?export=download&id=DOCUMENT_ID" > /tmp/intermezzo.html
curl -L -b /tmp/cookies "https://drive.google.com$(cat /tmp/intermezzo.html | grep -Po 'uc-download-link" [^>]* href="\K[^"]*' | sed 's/\&amp;/\&/g')" > FINAL_DOWNLOADED_FILENAME

Notes:注意事项:

  • this procedure will probably stop working after some Google changes在 Google 进行一些更改后,此过程可能会停止工作
  • the grep command uses Perl syntax ( -P ) and the \\K "operator" which essentially means "do not include anything preceding \\K to the matched result. I don't know which version of grep introduced these options, but ancient or non-Ubuntu versions probably don't have it grep 命令使用 Perl 语法 ( -P ) 和\\K “运算符”,这实质上意味着“在匹配结果之前不包含\\K之前的任何内容。我不知道 grep 的哪个版本引入了这些选项,但古老的还是非-Ubuntu 版本可能没有
  • a Java solution would be more or less the same, just take a HTTPS library which can handle cookies, and some nice text-parsing library Java 解决方案或多或少是相同的,只需要一个可以处理 cookie 的 HTTPS 库和一些不错的文本解析库

I know this is an old question but I could not find a solution to this problem after some research, so I am sharing what worked for me.我知道这是一个老问题,但经过一些研究后我找不到解决这个问题的方法,所以我分享了对我有用的方法。

I have written this C# code for one of my projects.我为我的一个项目编写了这个 C# 代码。 It can bypass the scan virus warning programmatically.它可以以编程方式绕过扫描病毒警告。 The code can probably be converted to Java.代码可能可以转换为Java。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;

public class FileDownloader : IDisposable
{
    private const string GOOGLE_DRIVE_DOMAIN = "drive.google.com";
    private const string GOOGLE_DRIVE_DOMAIN2 = "https://drive.google.com";

    // In the worst case, it is necessary to send 3 download requests to the Drive address
    //   1. an NID cookie is returned instead of a download_warning cookie
    //   2. download_warning cookie returned
    //   3. the actual file is downloaded
    private const int GOOGLE_DRIVE_MAX_DOWNLOAD_ATTEMPT = 3;

    public delegate void DownloadProgressChangedEventHandler( object sender, DownloadProgress progress );

    // Custom download progress reporting (needed for Google Drive)
    public class DownloadProgress
    {
        public long BytesReceived, TotalBytesToReceive;
        public object UserState;

        public int ProgressPercentage
        {
            get
            {
                if( TotalBytesToReceive > 0L )
                    return (int) ( ( (double) BytesReceived / TotalBytesToReceive ) * 100 );

                return 0;
            }
        }
    }

    // Web client that preserves cookies (needed for Google Drive)
    private class CookieAwareWebClient : WebClient
    {
        private class CookieContainer
        {
            private readonly Dictionary<string, string> cookies = new Dictionary<string, string>();

            public string this[Uri address]
            {
                get
                {
                    string cookie;
                    if( cookies.TryGetValue( address.Host, out cookie ) )
                        return cookie;

                    return null;
                }
                set
                {
                    cookies[address.Host] = value;
                }
            }
        }

        private readonly CookieContainer cookies = new CookieContainer();
        public DownloadProgress ContentRangeTarget;

        protected override WebRequest GetWebRequest( Uri address )
        {
            WebRequest request = base.GetWebRequest( address );
            if( request is HttpWebRequest )
            {
                string cookie = cookies[address];
                if( cookie != null )
                    ( (HttpWebRequest) request ).Headers.Set( "cookie", cookie );

                if( ContentRangeTarget != null )
                    ( (HttpWebRequest) request ).AddRange( 0 );
            }

            return request;
        }

        protected override WebResponse GetWebResponse( WebRequest request, IAsyncResult result )
        {
            return ProcessResponse( base.GetWebResponse( request, result ) );
        }

        protected override WebResponse GetWebResponse( WebRequest request )
        {
            return ProcessResponse( base.GetWebResponse( request ) );
        }

        private WebResponse ProcessResponse( WebResponse response )
        {
            string[] cookies = response.Headers.GetValues( "Set-Cookie" );
            if( cookies != null && cookies.Length > 0 )
            {
                int length = 0;
                for( int i = 0; i < cookies.Length; i++ )
                    length += cookies[i].Length;

                StringBuilder cookie = new StringBuilder( length );
                for( int i = 0; i < cookies.Length; i++ )
                    cookie.Append( cookies[i] );

                this.cookies[response.ResponseUri] = cookie.ToString();
            }

            if( ContentRangeTarget != null )
            {
                string[] rangeLengthHeader = response.Headers.GetValues( "Content-Range" );
                if( rangeLengthHeader != null && rangeLengthHeader.Length > 0 )
                {
                    int splitIndex = rangeLengthHeader[0].LastIndexOf( '/' );
                    if( splitIndex >= 0 && splitIndex < rangeLengthHeader[0].Length - 1 )
                    {
                        long length;
                        if( long.TryParse( rangeLengthHeader[0].Substring( splitIndex + 1 ), out length ) )
                            ContentRangeTarget.TotalBytesToReceive = length;
                    }
                }
            }

            return response;
        }
    }

    private readonly CookieAwareWebClient webClient;
    private readonly DownloadProgress downloadProgress;

    private Uri downloadAddress;
    private string downloadPath;

    private bool asyncDownload;
    private object userToken;

    private bool downloadingDriveFile;
    private int driveDownloadAttempt;

    public event DownloadProgressChangedEventHandler DownloadProgressChanged;
    public event AsyncCompletedEventHandler DownloadFileCompleted;

    public FileDownloader()
    {
        webClient = new CookieAwareWebClient();
        webClient.DownloadProgressChanged += DownloadProgressChangedCallback;
        webClient.DownloadFileCompleted += DownloadFileCompletedCallback;

        downloadProgress = new DownloadProgress();
    }

    public void DownloadFile( string address, string fileName )
    {
        DownloadFile( address, fileName, false, null );
    }

    public void DownloadFileAsync( string address, string fileName, object userToken = null )
    {
        DownloadFile( address, fileName, true, userToken );
    }

    private void DownloadFile( string address, string fileName, bool asyncDownload, object userToken )
    {
        downloadingDriveFile = address.StartsWith( GOOGLE_DRIVE_DOMAIN ) || address.StartsWith( GOOGLE_DRIVE_DOMAIN2 );
        if( downloadingDriveFile )
        {
            address = GetGoogleDriveDownloadAddress( address );
            driveDownloadAttempt = 1;

            webClient.ContentRangeTarget = downloadProgress;
        }
        else
            webClient.ContentRangeTarget = null;

        downloadAddress = new Uri( address );
        downloadPath = fileName;

        downloadProgress.TotalBytesToReceive = -1L;
        downloadProgress.UserState = userToken;

        this.asyncDownload = asyncDownload;
        this.userToken = userToken;

        DownloadFileInternal();
    }

    private void DownloadFileInternal()
    {
        if( !asyncDownload )
        {
            webClient.DownloadFile( downloadAddress, downloadPath );

            // This callback isn't triggered for synchronous downloads, manually trigger it
            DownloadFileCompletedCallback( webClient, new AsyncCompletedEventArgs( null, false, null ) );
        }
        else if( userToken == null )
            webClient.DownloadFileAsync( downloadAddress, downloadPath );
        else
            webClient.DownloadFileAsync( downloadAddress, downloadPath, userToken );
    }

    private void DownloadProgressChangedCallback( object sender, DownloadProgressChangedEventArgs e )
    {
        if( DownloadProgressChanged != null )
        {
            downloadProgress.BytesReceived = e.BytesReceived;
            if( e.TotalBytesToReceive > 0L )
                downloadProgress.TotalBytesToReceive = e.TotalBytesToReceive;

            DownloadProgressChanged( this, downloadProgress );
        }
    }

    private void DownloadFileCompletedCallback( object sender, AsyncCompletedEventArgs e )
    {
        if( !downloadingDriveFile )
        {
            if( DownloadFileCompleted != null )
                DownloadFileCompleted( this, e );
        }
        else
        {
            if( driveDownloadAttempt < GOOGLE_DRIVE_MAX_DOWNLOAD_ATTEMPT && !ProcessDriveDownload() )
            {
                // Try downloading the Drive file again
                driveDownloadAttempt++;
                DownloadFileInternal();
            }
            else if( DownloadFileCompleted != null )
                DownloadFileCompleted( this, e );
        }
    }

    // Downloading large files from Google Drive prompts a warning screen and requires manual confirmation
    // Consider that case and try to confirm the download automatically if warning prompt occurs
    // Returns true, if no more download requests are necessary
    private bool ProcessDriveDownload()
    {
        FileInfo downloadedFile = new FileInfo( downloadPath );
        if( downloadedFile == null )
            return true;

        // Confirmation page is around 50KB, shouldn't be larger than 60KB
        if( downloadedFile.Length > 60000L )
            return true;

        // Downloaded file might be the confirmation page, check it
        string content;
        using( var reader = downloadedFile.OpenText() )
        {
            // Confirmation page starts with <!DOCTYPE html>, which can be preceeded by a newline
            char[] header = new char[20];
            int readCount = reader.ReadBlock( header, 0, 20 );
            if( readCount < 20 || !( new string( header ).Contains( "<!DOCTYPE html>" ) ) )
                return true;

            content = reader.ReadToEnd();
        }

        int linkIndex = content.LastIndexOf( "href=\"/uc?" );
        if( linkIndex < 0 )
            return true;

        linkIndex += 6;
        int linkEnd = content.IndexOf( '"', linkIndex );
        if( linkEnd < 0 )
            return true;

        downloadAddress = new Uri( "https://drive.google.com" + content.Substring( linkIndex, linkEnd - linkIndex ).Replace( "&amp;", "&" ) );
        return false;
    }

    // Handles the following formats (links can be preceeded by https://):
    // - drive.google.com/open?id=FILEID
    // - drive.google.com/file/d/FILEID/view?usp=sharing
    // - drive.google.com/uc?id=FILEID&export=download
    private string GetGoogleDriveDownloadAddress( string address )
    {
        int index = address.IndexOf( "id=" );
        int closingIndex;
        if( index > 0 )
        {
            index += 3;
            closingIndex = address.IndexOf( '&', index );
            if( closingIndex < 0 )
                closingIndex = address.Length;
        }
        else
        {
            index = address.IndexOf( "file/d/" );
            if( index < 0 ) // address is not in any of the supported forms
                return string.Empty;

            index += 7;

            closingIndex = address.IndexOf( '/', index );
            if( closingIndex < 0 )
            {
                closingIndex = address.IndexOf( '?', index );
                if( closingIndex < 0 )
                    closingIndex = address.Length;
            }
        }

        return string.Concat( "https://drive.google.com/uc?id=", address.Substring( index, closingIndex - index ), "&export=download" );
    }

    public void Dispose()
    {
        webClient.Dispose();
    }
}

And here's how you can use it:以下是如何使用它:

// NOTE: FileDownloader is IDisposable!
FileDownloader fileDownloader = new FileDownloader();

// This callback is triggered for DownloadFileAsync only
fileDownloader.DownloadProgressChanged += ( sender, e ) => Console.WriteLine( "Progress changed " + e.BytesReceived + " " + e.TotalBytesToReceive );
// This callback is triggered for both DownloadFile and DownloadFileAsync
fileDownloader.DownloadFileCompleted += ( sender, e ) => Console.WriteLine( "Download completed" );

fileDownloader.DownloadFileAsync( "https://INSERT_DOWNLOAD_LINK_HERE", @"C:\downloadedFile.txt" );

#Case 1: download file with small size. #Case 1:下载小文件。

#Case 2: download file with large size. #Case 2:下载大文件。

  • You stuck a wall of a virus scan alert page returned.你贴了一堵墙的病毒扫描警报页面返回。 By parsing html dom element, I tried to get link with confirm code under button "Download anyway" but it didn't work.通过解析 html dom 元素,我尝试在“仍然下载”按钮下获取带有确认代码的链接,但没有成功。 Its may required cookie or session info.它可能需要 cookie 或会话信息。 enter image description here在此处输入图片说明

SOLUTION:解决方案:

  • Finally I found solution for two above cases.最后我找到了上述两种情况的解决方案。 Just need to put httpConnection.setDoOutput(true) in connection step to get a Json.只需要在连接步骤中放置httpConnection.setDoOutput(true)即可获得 Json。

    )]}' { "disposition":"SCAN_CLEAN", "downloadUrl":"http:www...", "fileName":"exam_list_json.txt", "scanResult":"OK", "sizeBytes":2392}

Then, you can use any Json parser to read downloadUrl, fileName and sizeBytes.然后,您可以使用任何 Json 解析器读取 downloadUrl、fileName 和 sizeBytes。

  • You can refer follow snippet, hope it help.您可以参考以下代码段,希望对您有所帮助。

     private InputStream gConnect(String remoteFile) throws IOException{ URL url = new URL(remoteFile); URLConnection connection = url.openConnection(); if(connection instanceof HttpURLConnection){ HttpURLConnection httpConnection = (HttpURLConnection) connection; connection.setAllowUserInteraction(false); httpConnection.setInstanceFollowRedirects(true); httpConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); httpConnection.setDoOutput(true); httpConnection.setRequestMethod("GET"); httpConnection.connect(); int reqCode = httpConnection.getResponseCode(); if(reqCode == HttpURLConnection.HTTP_OK){ InputStream is = httpConnection.getInputStream(); Map<String, List<String>> map = httpConnection.getHeaderFields(); List<String> values = map.get("content-type"); if(values != null && !values.isEmpty()){ String type = values.get(0); if(type.contains("text/html")){ String cookie = httpConnection.getHeaderField("Set-Cookie"); String temp = Constants.getPath(mContext, Constants.PATH_TEMP) + "/temp.html"; if(saveGHtmlFile(is, temp)){ String href = getRealUrl(temp); if(href != null){ return parseUrl(href, cookie); } } } else if(type.contains("application/json")){ String temp = Constants.getPath(mContext, Constants.PATH_TEMP) + "/temp.txt"; if(saveGJsonFile(is, temp)){ FileDataSet data = JsonReaderHelper.readFileDataset(new File(temp)); if(data.getPath() != null){ return parseUrl(data.getPath()); } } } } return is; } } return null; }

And并且

   public static FileDataSet readFileDataset(File file) throws IOException{
        FileInputStream is = new FileInputStream(file);
        JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));

        reader.beginObject();
        FileDataSet rs = new FileDataSet();
        while(reader.hasNext()){
            String name = reader.nextName();
            if(name.equals("downloadUrl")){
                rs.setPath(reader.nextString());
            } else if(name.equals("fileName")){
                rs.setName(reader.nextString());
            } else if(name.equals("sizeBytes")){
                rs.setSize(reader.nextLong());
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return rs;

    }

This seems to be updated again as of May 19, 2015:这似乎在 2015 年 5 月 19 日再次更新:

How I got it to work:我是如何让它工作的:

As in jmbertucci's recently updated answer, make your folder public to everyone.就像 jmbertucci 最近更新的答案一样,将您的文件夹公开给所有人。 This is a bit more complicated than before, you have to click Advanced to change the folder to "On - Public on the web."这比以前复杂一些,您必须单击“高级”将文件夹更改为“在网络上公开”。

Find your folder UUID as before--just go into the folder and find your UUID in the address bar:像以前一样找到您的文件夹 UUID——只需进入文件夹并在地址栏中找到您的 UUID:

https://drive.google.com/drive/folders/<folder UUID>

Then head to然后前往

https://googledrive.com/host/<folder UUID>

It will redirect you to an index type page with a giant subdomain, but you should be able to see the files in your folder.它会将您重定向到具有巨大子域的索引类型页面,但您应该能够看到文件夹中的文件。 Then you can right click to save the link to the file you want (I noticed that this direct link also has this big subdomain for googledrive.com ).然后您可以右键单击以将链接保存到您想要的文件(我注意到这个直接链接也有googledrive.com这个大子域)。 Worked great for me with wget .使用wget对我很有用。

This also seems to work with others' shared folders.这似乎也适用于其他人的共享文件夹。

eg,例如,

https://drive.google.com/folderview?id=0B7l10Bj_LprhQnpSRkpGMGV2eE0&usp=sharing https://drive.google.com/folderview?id=0B7l10Bj_LprhQnpSRkpGMGV2eE0&usp=sharing

maps to映射到

https://googledrive.com/host/0B7l10Bj_LprhQnpSRkpGMGV2eE0 https://googledrive.com/host/0B7l10Bj_LprhQnpSRkpGMGV2eE0

And a right click can save a direct link to any of those files.右键单击可以保存指向任何这些文件的直接链接。

使用服务帐户可能对您有用。

如果您只想以编程方式(与向用户提供在浏览器中打开的链接相反)通过 Google Drive API 下载文件,我建议使用文件的downloadUrl而不是webContentLink ,如此处所述: https: //developers.google.com/drive/web/manage-downloads

https://github.com/google/skicka https://github.com/google/skicka

I used this command line tool to download files from Google Drive.我使用这个命令行工具从 Google Drive 下载文件。 Just follow the instructions in Getting Started section and you should download files from Google Drive in minutes.只需按照入门部分中的说明操作,您应该会在几分钟内从 Google Drive 下载文件。

Check this out:看看这个:

wget https://raw.githubusercontent.com/circulosmeos/gdown.pl/master/gdown.pl
chmod +x gdown.pl
./gdown.pl https://drive.google.com/file/d/FILE_ID/view TARGET_PATH

I would consider downloading from the link, scraping the page that you get to grab the confirmation link, and then downloading that.我会考虑从链接下载,抓取您获取确认链接的页面,然后下载它。

If you look at the "download anyway" URL it has an extra confirm query parameter with a seemingly randomly generated token.如果您查看“仍然下载” URL,它有一个额外的confirm查询参数,其中包含一个看似随机生成的令牌。 Since it's random...and you probably don't want to figure out how to generate it yourself, scraping might be the easiest way without knowing anything about how the site works.由于它是随机的……而且您可能不想弄清楚如何自己生成它,因此在不了解站点工作原理的情况下抓取可能是最简单的方法。

You may need to consider various scenarios.您可能需要考虑各种情况。

I simply create a javascript so that it automatically capture the link and download and close the tab with the help of tampermonkey .我只是创建了一个javascript,以便它在tampermonkey的帮助下自动捕获链接并下载并关闭选项卡。

// ==UserScript==
// @name         Bypass Google drive virus scan
// @namespace    SmartManoj
// @version      0.1
// @description  Quickly get the download link
// @author       SmartManoj
// @match        https://drive.google.com/uc?id=*&export=download*
// @grant        none
// ==/UserScript==

    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function demo() {
        await sleep(5000);
        window.close();
    }

    (function() {
        location.replace(document.getElementById("uc-download-link").href);
        demo();
    })();

Similarly you can get the html source of the url and download in java.同样的,你可以得到url的html源并用java下载。

I faced an issue in direct download because I was logged in using multiple Google accounts.我在直接下载时遇到了问题,因为我使用多个 Google 帐户登录。 Solution is append authUser=0 parameter.解决方案是附加authUser=0参数。 Sample request URL to download : https://drive.google.com/uc?id=FILEID&authuser=0&export=download要下载的示例请求 URL: https://drive.google.com/uc?id=FILEID&authuser=0&export=download : https://drive.google.com/uc?id=FILEID&authuser=0&export=download download

Update as of August 2020:截至 2020 年 8 月的更新:

This is what worked for me recently -这就是最近对我有用的东西-

Upload your file and get a shareable link which anyone can see(Change permission from "Restricted" to "Anyone with the Link" in the share link options)上传您的文件并获取任何人都可以看到的可共享链接(在共享链接选项中将权限从“受限”更改为“拥有链接的任何人”)

Then run:然后运行:

 SHAREABLE_LINK=<google drive shareable link>
 curl -L https://drive.google.com/uc\?id\=$(echo $SHAREABLE_LINK | cut -f6 -d"/")

For any shared link replace FILENAME and FILEID, (for very large files requiring confirmation):对于任何共享链接,替换 FILENAME 和 FILEID,(对于需要确认的非常大的文件):

wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O- | sed -rn 's/. confirm=([0-9A-Za-z_]+). /\\1\\n/p')&id=FILEID" -O FILENAME && rm -rf /tmp/cookies.txt wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep- session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O- | sed -rn 's/ .confirm=([0-9A-Za- z_]+). /\\1\\n/p')&id=FILEID" -O 文件名 && rm -rf /tmp/cookies.txt

(For small files): wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O FILENAME (对于小文件): wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O FILENAME

https://drive.google.com/uc?export=download&id=FILE_ID replace the FILE_ID with file id. https://drive.google.com/uc?export=download&id=FILE_ID将 FILE_ID 替换为文件 ID。

if you don't know were is file id then check this article Article LINK如果您不知道是文件 ID,请查看本文文章链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM