C# 服务帐户凭据是否可以使用json密钥而不是p12密钥?

C# 服务帐户凭据是否可以使用json密钥而不是p12密钥?,c#,google-bigquery,google-sheets-api,google-api-dotnet-client,C#,Google Bigquery,Google Sheets Api,Google Api Dotnet Client,我将“Google.api.Bigquery.v2客户端库”与C#一起使用 我授权使用“服务帐户”谷歌BigQuery(请参阅)。要创建X509证书,我使用Google开发者控制台中的p12密钥。但是,现在json键是默认的。我可以用它代替p12键吗 我有以下代码: string serviceAccountEmail = "xxxx@developer.gserviceaccount.com"; X509Certificate2 certificate; using (Stream

我将“Google.api.Bigquery.v2客户端库”与C#一起使用

我授权使用“服务帐户”谷歌BigQuery(请参阅)。要创建X509证书,我使用Google开发者控制台中的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();

我得到了这个链接,它表明在C#应用程序中使用JSON文件的服务帐户身份验证尚未添加到Google BigQuery API中

现在这是可能的(我使用了谷歌API的v1.13.1.0)

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);
谷歌表单示例:

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;
GoogleCredential凭证;
使用(Stream-Stream=newfilestream(@“mykey.json”、FileMode.Open、FileAccess.Read、FileShare.Read))
{
credential=GoogleCredential.FromStream(流);
}
credential=credential.createScope(新[]{
"https://spreadsheets.google.com/feeds", 
"https://docs.google.com/feeds" });
持弦人;
尝试
{
任务任务=((iTokeAccess)凭据).GetAccessTokenForRequestAsync();
task.Wait();
承载者=任务。结果;
}
捕获(聚合异常)
{
抛出ex.InnerException;
}
GDataRequestFactory requestFactory=新的GDataRequestFactory(“我的应用程序”);
Add(string.Format(CultureInfo.InvariantCulture,“Authorization:Bearer{0}”,Bearer));
电子表格服务=新的电子表格服务(“我的应用程序”);
service.RequestFactory=RequestFactory;

这对我来说很有用:

var scopes = new[] { DriveService.Scope.Drive };

ServiceAccountCredential credential;
using (Stream stream = new FileStream(@"C:\path\key.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    credential =
        GoogleCredential.FromStream(stream).CreateScoped(scopes).UnderlyingCredential as
            ServiceAccountCredential;
}

显然,您应该为
scopes
变量和键文件的路径提供自己的值。您还需要获得
Google.api.Auth.OAuth2
Nuget包,以及您计划使用凭证的任何其他特定于服务的包(在我的例子中是
Google.api.Drive.v3
)。

类似的(未回答的)问题是,您是否尝试过使用此流:但对于BQ?这实际上非常接近。您是否有类似的ServiceAccountCredential(而不是UserCredential)示例?查看源代码,我认为这是不可能的。阅读示例项目时,除了Json之外,他们对所有项目都使用ServiceAccountCredentials。请注意,“还没有”已经过去了。目前正在编写2019年8月的报告,这两种方法都运行良好。我想知道为什么与上面的其他示例相比,这一点如此简单。我要试试看。