I have implemented a Google Service account and added it as an authorized user on the sheet.
Next I use the following code to access the sheets feed:
const string gUser = "[email protected]";
const string certFile = "CERTFILE.p12";
static void Main(string[] args) {
var certificate = new X509Certificate2(certFile, "notasecret", X509KeyStorageFlags.Exportable);
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(gUser) {
Scopes = new[] { "https://spreadsheets.google.com/feeds/" }
}.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
return;
var requestFactory = new GDataRequestFactory(null);
requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);
var service = new SpreadsheetsService("MY-SHEETS-SVC") { RequestFactory = requestFactory };
SpreadsheetQuery query = new SpreadsheetQuery();
var sheetFeed = service.Query(query);
Console.WriteLine("Spreadsheet list:");
foreach (var sheet in sheetFeed.Entries) {
Console.WriteLine("\t--" + sheet.Title.Text);
}
Console.ReadLine();
}
However it does not return any sheets despite granting the service account edit permissions. I have gone as far as adding the service account to the Google Apps security group with no change in results. In the past creating the account (same apps domain etc) and granting it edit permissions in the sharing settings was all that was required. Has the API changed to require OAUTH only or is there a setting somewhere that I'm missing?