是否可以将 json 密钥而不是 p12 密钥用于服务帐户凭据? [英] Is it possible to use json key instead of p12 key for service account credentials?
问题描述
我在 C# 中使用Google.Apis.Bigquery.v2 客户端库".
I am using "Google.Apis.Bigquery.v2 Client Library" with C#.
我正在使用服务帐户"授权 Google BigQuery(请参阅 http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html).要创建 X509 证书,我使用 Google Developers Console 中的 p12 密钥.但是,现在 json 键是默认值.我可以用它代替 p12 键吗?
I am authorizing to Google BigQuery using "Service Account" (see http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html). To create the X509 certificate I use the p12 key from the Google Developers Console. However, right now the json key is the default. Can I use it instead the p12 key?
我有以下代码:
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();
推荐答案
现在可以了(我使用的是 Google API 1.13.1.0 版).
It is now possible (I used v 1.13.1.0 of Google APIs).
BigQuery 示例:
Example for BigQuery:
GoogleCredential credential;
using (Stream stream = new FileStream(@"C:mykey.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream);
}
string[] scopes = new string[] {
BigqueryService.Scope.Bigquery,
BigqueryService.Scope.CloudPlatform,
};
credential = credential.CreateScoped(scopes);
BaseClientService.Initializer initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = (IConfigurableHttpClientInitializer)credential,
ApplicationName = "My Application",
GZipEnabled = true,
};
BigqueryService service = new BigqueryService(initializer);
Google 表格示例:
Example for Google Sheets:
GoogleCredential credential;
using (Stream stream = new FileStream(@"mykey.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream);
}
credential = credential.CreateScoped(new[] {
"https://spreadsheets.google.com/feeds",
"https://docs.google.com/feeds" });
string bearer;
try
{
Task<string> task = ((ITokenAccess)credential).GetAccessTokenForRequestAsync();
task.Wait();
bearer = task.Result;
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
GDataRequestFactory requestFactory = new GDataRequestFactory("My Application");
requestFactory.CustomHeaders.Add(string.Format(CultureInfo.InvariantCulture, "Authorization: Bearer {0}", bearer));
SpreadsheetsService service = new SpreadsheetsService("My Application");
service.RequestFactory = requestFactory;
这篇关于是否可以将 json 密钥而不是 p12 密钥用于服务帐户凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!