0

I am trying to download file from Google Drive using ASPSnippets.GoogleAP.dll and Google.Apis.Drive.v3.dll. But facing some challenges. File is being downloaded, but it is in some type of weird HTML content.

I am using following code to download it :

GoogleConnect.ClientId = "xxxx.apps.googleusercontent.com";
GoogleConnect.ClientSecret = "xxxxxx";
GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
GoogleConnect.API = EnumAPI.Drive;
if (!string.IsNullOrEmpty(Request.QueryString["code"]))
    {
     string code = Request.QueryString["code"];
     string json = GoogleConnect.Fetch("me", code);
     GoogleDriveFiles files = new JavaScriptSerializer().Deserialize<GoogleDriveFiles>(json);

     //Remove the deleted files.
     var driveService = new DriveService();
     Label1.Text = theHiddenField1.Value;
     WebClient wb = new WebClient();
     wb.DownloadFile(theHiddenField1.Value, "C://" + fileName);
    }
    else if (Request.QueryString["error"] == "access_denied")
     { 
       ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);                           
     }
      else
      {   
        GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.readonly");              
       }  

Can someone please help me to resolve the issue?

abielita
  • 13,147
  • 2
  • 17
  • 59
vimal mishra
  • 1,087
  • 2
  • 12
  • 32
  • Can you post the error message you are getting? Or are you not getting an error, it is just not downloading? – lakeIn231 Mar 13 '17 at 17:46
  • @Jay266 I am not getting any error msg file is getting downloaded with soem html content. – vimal mishra Mar 13 '17 at 17:46
  • So what does the html say? – Alex K. Mar 13 '17 at 17:47
  • @AlexK. When I opend the html in browser page it is gmail login page. – vimal mishra Mar 13 '17 at 17:48
  • You may want to check this [tutorial](http://www.daimto.com/google-drive-api-c-download/) about *Google Drive API with C# .net – Download*. Also, in some cases, an incompatibility can occur between your Host and particular file types. When clicking a link to a File Download, the file does not actually download, and instead a large quantity of garbled text appears in your web browser. Make sure that you have properly accommodate files of that type formats listed [here](https://developers.google.com/drive/v3/web/integrate-open#open_and_convert_google_docs_in_your_app). – abielita Mar 14 '17 at 08:19

1 Answers1

0

I'm not a C# developer, but if you're using Google.Apis.Drive.v3, then that is the latest version of the Google Drive API. I don't see any part of your code that references the Google APIs Client Library for .NET, but that's the recommended way to talk to modern Google APIs.

If you've installed it, take a look at the C# quickstart sample for the Drive API. Then check out the C#/.NET examples on the Download Files page in the docs, and it should lead you to a working solution. In your other comment, you asked about passing auth tokens, so if you're using the client library, you shouldn't have to worry about that part. On that docs page, you'll find a sample for regular file downloads and another for exporting Google documents (Docs, Sheets, Slides).

Finally, for additional reference, here are the .NET reference docs for the Drive API and the .NET Google APIs Client Library developers guide.

wescpy
  • 10,689
  • 3
  • 54
  • 53