How to download a file from Google Drive

2019-03-29 11:34发布

问题:

I am currently trying to make an application that downloads files from Google Drive.

In the Google Drive SDK documentation I found an example method that should return a System.IO.Stream object.

https://developers.google.com/drive/manage-downloads

To do so it needs the download URL for each file. However, this download URL is not always specified returned. Does anyone have any Idea why this is? And what I can do about it?

Update: I have found that I can use an exportURL. These are almost always returned by the service.

Folowup question: How can I run an app as a certain user? => sometimes the app makes a request to a url and then the request times out. When I try that same URL in a browser it works like a charm...

Thanks!

回答1:

You won't have a downloadlink if the file you are trying to download is a native google docs format. If that's the case you must look for the exportLinks.

You will have several export links, so you will have to choose which format suits you best : docx or odt for document for example



回答2:

Might be late but I just downloaded from Google Drive using the following code:

var stream = service.HttpClient.GetStreamAsync(downloadUrl);
        var result = stream.Result;
        using (var fileStream = System.IO.File.Create(filePath))
        {
            result.CopyTo(fileStream);
        }