Getting started with Google Drive api c#, trying to download file

1,816 views
Skip to first unread message

jonas.g...@wexer.com

unread,
Jan 23, 2017, 10:56:22 AM1/23/17
to Google APIs Explorer Users Forum
Hello,

So today I've started looking at the Google Drive api since i wanna integrate google drive in my application.

I followed the quick start guide for .NET and received my access token from the program that I was told to copy/paste from the guide.

My next step is to try and download a file from my google drive account. I go to google drive, fetch the id, copy&paste the token from earlier and create a simple c# program as follows:

           HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("https://www.googleapis.com/drive/v3/files/fileId?alt=media");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "accessTokenFromQuickstartprogram");
            client.DefaultRequestHeaders.Accept.Clear();
            HttpRequestMessage sendRequest = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress);
            HttpResponseMessage response = client.SendAsync(sendRequest).Result;

            string result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(response);
            Console.WriteLine(result);

I followed the documentation here (https://developers.google.com/drive/v3/web/manage-downloads), add a ?alt=media to the URL in order to download and authorize my request by adding the header. From this request i get status code "200 - OK" but there's no response but the string in the text file that i tried to download  -  i kinda expected a download URL of some sort since im trying to download the file?

Entire response:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  X-GUploader-UploadID: someNumber
  Vary: Origin
  Vary: X-Origin
  X-Goog-Hash: crc32c=z35Mtg==
  Alt-Svc: quic=":443"; ma=2592000; v="35,34"
  Cache-Control: must-revalidate, max-age=0, private
  Date: Mon, 23 Jan 2017 15:53:22 GMT
  Server: UploadServer
  Content-Length: 14
  Content-Disposition: attachment
  Content-Type: text/plain
  Expires: Mon, 23 Jan 2017 15:53:22 GMT
}

Am I doing something wrong trying to download a file using the Google Drive api? Thoughts?

John Boswell

unread,
Jan 23, 2017, 8:55:36 PM1/23/17
to Google APIs Explorer Users Forum
I believe ?alt=media is supposed to return the file contents, so they can programmatically be written to a file, as in the example scripts on the page you linked to. If you instead want a link to the file for download, query instead with ?fields=webContentLink. This will return the normal JSON result with a webContentLink field containing the URL to download your file directly.

jonas.g...@wexer.com

unread,
Jan 24, 2017, 3:17:35 AM1/24/17
to Google APIs Explorer Users Forum
In the example they use a download function from the Google Drive Api it seems, do i need to redesign my program using that or can i get the file (somehow) using the classic HttpClient lib in c#?

John Boswell

unread,
Jan 24, 2017, 4:48:16 PM1/24/17
to Google APIs Explorer Users Forum
I suspect that you'd need to re-implement authentication functionality (passing the bearer token in the request header) if you tried to use the classic HttpClient lib in C#. That should be possible but it will be more work.
Reply all
Reply to author
Forward
0 new messages