Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file from Google Drive

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!

like image 850
Nielsm Avatar asked Oct 06 '22 05:10

Nielsm


2 Answers

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);
        }
like image 95
Sando Avatar answered Oct 21 '22 11:10

Sando


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

like image 1
Jerome Avatar answered Oct 21 '22 10:10

Jerome