Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I need to download files from google drive and save to a folder on local using C#.
I have done as described in Google Developer API documentation (https://developers.google.com/drive/v3/web/manage-downloads) but i'm getting the file with invalid format. Please suggest how to download it.

What I have tried:

downloadUrl = url of the file (eg: https://drive.google.com/uc?id=1ULNeKdDoCRmWgPPBW8-d1EGUZgqvA1Ul&export=download)
filename = some name with extension

C#
private bool SaveToDir(string downloadUrl,string filename) {
string filePath = Server.MapPath("imports/");
bool resp = false;
DriveService ds = new DriveService();
Uri temp = new Uri(downloadUrl);
string fileId = HttpUtility.ParseQueryString(temp.Query).Get("id");
var req = ds.Files.Get(fileId.Trim());
var stream = new MemoryStream();
req.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress dp)=> {
switch (dp.Status)
{
 case Google.Apis.Download.DownloadStatus.Downloading:
      Message("downloading, please wait....");
      break;
 case Google.Apis.Download.DownloadStatus.Completed:
      using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                stream.WriteTo(file);
                Message("File Downloaded successfully.");
            }
      resp = true;
      break;
 case Google.Apis.Download.DownloadStatus.Failed:
      Message("Failed to Download.");
      resp = false;
      break;
 }
 };
 req.Download(stream);
Posted
Updated 20-Feb-18 19:14pm
Comments
Afzaal Ahmad Zeeshan 16-Feb-18 3:01am    
What invalid format are you receiving?
cgprakash 21-Feb-18 0:27am    
suppose if I select the doc. file to download, the file is end with .doc but if i try to open it shows invalid format and if i try to open in vs code IDE it shows the html of google signin page.

1 solution

That is because the file itself is corrupt, as it is not a .doc file rather an HTML document to sign in to Google that has been downloaded as the file you wanted to download.

What you need to do is, check whether Google is authenticating your request or not. You did not include the code to authenticate the users; — how are you doing that? OAuth2? Key?

That entirely depends on how you authenticate the users, perhaps start here, About Authorization  |  Drive REST API  |  Google Developers[^]. Also a small excerpt from their page,
Quote:
Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you.
But how Google Sign-In helps you out, I am unsure of that in this context, thus OAuth2 would be a good way to go.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900