我已经实现了一个Google服务帐户,并将其作为授权用户添加到工作表中。
接下来,我使用以下代码访问工作表进纸:
const string gUser = "SVC-ACCOUNT@developer.gserviceaccount.com";
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();
}
但是,尽管授予服务帐户编辑权限,它不会返回任何工作表。我已经将服务帐户添加到Google Apps安全组,结果没有变化。过去,只需创建帐户(相同的应用程序域等)并在共享设置中授予其编辑权限即可。API 是否已更改为仅需要 OAUTH,或者是否有我缺少的设置?
我的应用域正在更改,目前正在迁移中。在新帐户下添加了一个工作表,它立即出现。