I'm trying to export a Google Spreadsheet as a .xlsx file. To do so, I've started using Google.Apis.Sheets.v4 library. There I'm creating SheetsService and using Google.Apis.Services.IClientService.HttpClient to do the request to the Google API.
https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key={0}&gid={1}&exportFormat=xlsx
It works correctly with public documents, but I'm facing an issue with private spreadsheets.
For authentication, my application uses a service account with the public key, and from the spreadsheets UI, I gave access to that document for that account.
The fun thing is I can request information from that spreadsheet via Google.Apis.Sheets.v4.SpreadsheetsResource.Get method.
My question is: Why I can't access a document via HttpClient and can do that via SheetsService?
The code sample I use to do the call:
using (var service = GoogleSheetsHelper.GetService(settings))
{
var exportUrl = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key={0}&gid={1}&exportFormat=xlsx";
var requestResult = await service.HttpClient.GetAsync(exportUri).ConfigureAwait(false);
if (!requestResult.IsSuccessStatusCode)
{
throw new Exception();
}
return await requestResult.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
What is also interesting - requestResult.IsSuccessStatusCode is always true! Sometimes it just returns html with the google authentication page instead of .xlsx file.
SheetsService.Scope.SpreadsheetsReadonly. During debug, I've also triedSheetsService.Scope.Spreadsheets, but no luck here. I'll try Drive scopes, maybe they are responsible for the file export. - Dmitrii GvozdevSheetsService.Scope.Spreadsheets, SheetsService.Scope.Driveand it didn't work with two widest scopes. Probably that is not the cause of an issue - Dmitrii Gvozdev