selva kumar
selva kumar

Reputation: 1156

Google BigQuery Service Account Credentials using JSON file in C# application

While Creating Service Account for Google BigQuery, There are two key file type. 1. P12 Key File 2. JSON Key File.

I can able to connect Google BigQuery with Service Account Credentials using P12 Key File by using following code.

String serviceAccountEmail = "[email protected]";

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

Now i am trying to connect Service Account Credentials using JSON file type, but i could not get the proper syntax for creating.

How can we connect Google BigQuery with Service Account Credentials using JSON File?

Thanks,

Upvotes: 3

Views: 3387

Answers (2)

Andrey Belykh
Andrey Belykh

Reputation: 2654

It is now possible (I used v 1.13.1.0 of Google APIs).

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

Upvotes: 1

selva kumar
selva kumar

Reputation: 1156

I got the link, Which indicates Service Account Authentication using JSON file in C# application is not yet added in Google BigQuery API, So i would like to close the question.

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

Upvotes: 2

Related Questions