是否有可能使用JSON的关键,而不是为服务帐户凭据P12关键? [英] Is it possible to use json key instead of p12 key for service account credentials?
问题描述
我使用Google.Apis.Bigquery.v2客户端库用C#。
我使用的服务帐户授权给谷歌的BigQuery(见<一HREF =http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html> http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html )。要创建我使用来自谷歌开发者控制台的P12键X509证书。然而,现在的关键JSON是默认的。我可以用它代替P12键
我有以下代码:
字符串serviceAccountEmail =xxxx@developer.gserviceaccount.com;
X509Certificate2证书;使用
(流流=新的FileStream(@C:\key.p12,FileMode.Open,FileAccess.Read,FileShare.Read))
{使用
(MemoryStream的毫秒=新的MemoryStream())
{
stream.CopyTo(毫秒);
证书=新X509Certificate2(ms.ToArray(),notasecret,X509KeyStorageFlags.Exportable);
}
}
//创建凭据
ServiceAccountCredential证书=新ServiceAccountCredential(
新ServiceAccountCredential.Initializer(serviceAccountEmail)
{
作用域=新[] {
BigqueryService.Scope.Bigquery,
BigqueryService.Scope.CloudPlatform,
},
} .FromCertificate(证书));
//创建服务
BaseClientService.Initializer初始化=新BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
应用程序名称=我的应用程序,
GZipEnabled = TRUE,
};
BigqueryService服务=新BigqueryService(初始化);
VAR项目= service.Projects.List()执行();
现在有可能(我用v 1.13.1.0对于大量查询的谷歌的API)
例如:
GoogleCredential凭证;
使用(流流=新的FileStream(@C:\mykey.json,FileMode.Open,FileAccess.Read,FileShare.Read))
{
=凭证GoogleCredential.FromStream (流);
}
的String [] =范围新的String [] {
BigqueryService.Scope.Bigquery,
BigqueryService.Scope.CloudPlatform,
};
凭据= credential.CreateScoped(范围);
BaseClientService.Initializer初始化=新BaseClientService.Initializer()
{
HttpClientInitializer =(IConfigurableHttpClientInitializer)认证,
应用程序名称=我的应用,
GZipEnabled = TRUE,
};
BigqueryService服务=新BigqueryService(初始化);对于谷歌表
例如:
GoogleCredential凭证;
使用(流流=新的FileStream(@mykey.json,FileMode.Open,FileAccess.Read,FileShare.Read))
{
凭据= GoogleCredential.FromStream(流);
}
凭据= credential.CreateScoped(新[] {
https://spreadsheets.google.com/feeds,
https://docs.google。 COM /供稿});
串承载;
试
{
任务<串GT;任务=((ITokenAccess)证书).GetAccessTokenForRequestAsync();
task.Wait();
承载= task.Result;
}
赶上(AggregateException前)
{
扔ex.InnerException;
}
GDataRequestFactory requestFactory =新GDataRequestFactory(我的应用程序);
requestFactory.CustomHeaders.Add(的String.Format(CultureInfo.InvariantCulture,授权:承载{0},承载));
SpreadsheetsService服务=新SpreadsheetsService(我的应用程序);
service.RequestFactory = requestFactory;
I am using "Google.Apis.Bigquery.v2 Client Library" with C#.
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?
I have the following code:
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();
It is now possible (I used v 1.13.1.0 of Google APIs).
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);
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屋!