Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have tested this against the Google.Apis.Drive.v2 Client Library nuget package version 1.9.2.1890. One thing to note, however, is that you are now required to register a developer's account with the Google API and setup API access and credentials for your application through the Google Developer's Console found <a href="https://console.developers.google.com" rel="nofollow">here</a>.</p> <p>If you want to just use this for a service account aka not access files on behalf of the user but for automation or internal tools, use something like this:</p> <p>(If I remember correctly service accounts still require a one-time OAuth approval interaction by a human via the OAuth browser pop-up to add the app to the authorized apps on the service account)</p> <pre><code>private const string SERVICE_ACCOUNT_EMAIL = "YOUR_SERVICE_ACCOUNT_EMAIL_HERE"; //looks like XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX@developer.gserviceaccount.com; static DriveService BuildServiceAccountService() { var certificate = new X509Certificate2(PATH_TO_YOUR_X509_CERT, "notasecret", X509KeyStorageFlags.Exportable); var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL) { Scopes = new[] { DriveService.Scope.Drive }, User = "ACTUAL_EMAIL_ADDRESS" // this should be the normal xxxxxx@gmail account that has the google drive files }.FromCertificate(certificate)); // Create the service. var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Drive API Service Account Sample", }); return service; } public static void DownloadSpreadsheetAsXlsx(string spreadsheetName, string filePath) { var service = BuildServiceAccountService(); var request = service.Files.List(); request.Q = String.Format("title = '{0}'", spreadsheetName); var files = request.Execute(); var file = files.Items.FirstOrDefault(); var dlUrl = String.Format("https://docs.google.com/spreadsheets/d/{0}/export?format=xlsx&amp;id={0}", file.Id); File.WriteAllBytes(filePath, service.HttpClient.GetByteArrayAsync(dlUrl).Result); } </code></pre> <p>To access files on behalf of the user, you will need something like this to build your service instead:</p> <pre><code>static DriveService BuildUserAccountService(string userEmail) { UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "YOUR_CLIENT_ID", // your client Id ClientSecret = "YOUR_CLIENT_SECRET", // Your client secret }, new[] { DriveService.Scope.Drive }, userEmail, CancellationToken.None).Result; // Create the service. var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Drive API User Account Sample", }); return service; } </code></pre> <p><strong>Old Answer for posterity's sake (The old Google GData APIs have been deprecated and anything below this point is no longer relevant and should not be expected to work):</strong></p> <p>I was having the same problem with finding the document key. I ended up finding it in the AlternateUri. Here is what I did (this has been compiled and confirmed to work in a new project and adding GData.Spreadsheets with NuGet):</p> <pre><code>//Use your authentication method here: SpreadsheetsService service = new SpreadsheetsService("DownloadSpreadsheet"); service.setUserCredentials("your username", "your password"); SpreadsheetQuery query = new SpreadsheetQuery(); SpreadsheetFeed feed = service.Query(query); SpreadsheetEntry fileEntry = feed.Entries.Cast&lt;SpreadsheetEntry&gt;().FirstOrDefault(entry =&gt; entry.Title.Text == "Name of spreadsheet"); //This is the good part string key = fileEntry.AlternateUri.Content.Substring(fileEntry.AlternateUri.Content.IndexOf("?key=")); string dlUrl = "https://docs.google.com/feeds/download/spreadsheets/Export" + key + "&amp;exportFormat=xlsx&amp;format=xlsx"; Stream stream = service.Query(new Uri(dlUrl)); using (FileStream fstream = new FileStream("something.xlsx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { stream.CopyTo(fstream); fstream.Flush(); } </code></pre> <p>I should mention that this requires LINQ (.NET Framework 3.5 or higher) due to the <code>.Cast&lt;SpreadsheetEntry&gt;().FirstOrDefault()</code> line. This can be done without LINQ, you will just need to manually enumerate the spreadsheets and find the one you're looking for instead of using a single line of code to do it. I just wanted to make sure everyone was aware before they started reporting that it does not work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload