使用C#应用程序中的JSON文件的Google BigQuery服务帐户凭据

时间:2015-06-17 06:48:43

标签: google-bigquery

在为Google BigQuery创建服务帐户时,有两种关键文件类型。 1. P12密钥文件2. JSON密钥文件。

我可以使用以下代码使用P12密钥文件将Google BigQuery与服务帐户凭据相关联。

String serviceAccountEmail =“XXXX@developer.gserviceaccount.com”;

            var certificate = new X509Certificate2(@"FileName.p12", "Secret Key", X509KeyStorageFlags.Exportable);

            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
               {
                   Scopes = new[] { BigqueryService.Scope.Bigquery, BigqueryService.Scope.BigqueryInsertdata, BigqueryService.Scope.CloudPlatform, BigqueryService.Scope.DevstorageFullControl }
               }.FromCertificate(certificate));

            BigqueryService Service = new BigqueryService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "PROJECT NAME"
            });

现在我正在尝试使用JSON文件类型连接服务帐户凭据,但我无法获得正确的创建语法。

如何使用JSON文件将Google BigQuery与服务帐户凭据相关联?

谢谢,

2 个答案:

答案 0 :(得分:2)

我收到了链接,表示使用C#应用程序中的JSON文件进行的服务帐户身份验证尚未添加到Google BigQuery API中,所以我想关闭这个问题。

https://github.com/google/google-api-dotnet-client/issues/533

答案 1 :(得分:0)

现在可以(我使用v 1.13.1.0的Google API)。

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);