【发布时间】:2015-10-28 17:05:06
【问题描述】:
我在 C# 中使用“Google.Apis.Bigquery.v2 客户端库”。
我正在使用“服务帐户”授权 Google BigQuery(请参阅 http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html)。要创建 X509 证书,我使用 Google Developers Console 中的 p12 密钥。但是,现在 json 键是默认值。我可以用它代替 p12 键吗?
我有以下代码:
string serviceAccountEmail = "xxxx@developer.gserviceaccount.com";
X509Certificate2 certificate;
using (Stream stream = new FileStream(@"C:\key.p12", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
certificate = new X509Certificate2(ms.ToArray(), "notasecret", X509KeyStorageFlags.Exportable);
}
}
// Create credentials
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] {
BigqueryService.Scope.Bigquery,
BigqueryService.Scope.CloudPlatform,
},
}.FromCertificate(certificate));
// Create the service
BaseClientService.Initializer initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "My Application",
GZipEnabled = true,
};
BigqueryService service = new BigqueryService(initializer);
var projects = service.Projects.List().Execute();
【问题讨论】:
-
类似(未回答)的问题在这里stackoverflow.com/questions/30884184
-
您是否尝试过使用此流程:stackoverflow.com/questions/19977864/… 但对于 BQ?
-
这个其实很接近了。您有 ServiceAccountCredential(不是 UserCredential)的类似示例吗?
-
查看源代码我认为不可能。阅读示例项目,他们使用 ServiceAccountCredentials,除了 Json。 github.com/google/google-api-dotnet-client/blob/…cloud.google.com/storage/docs/json_api/v1/…
标签: c# google-bigquery google-sheets-api google-api-dotnet-client