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:
Example for Google Sheets:
I got the link, Which indicates Service Account Authentication using JSON file in C# application is not yet added in Google BigQuery API.
https://github.com/google/google-api-dotnet-client/issues/533
This is working for me:
Obviously, you would provide your own values for the
scopes
variable and for the path to the key file. You would also need to get theGoogle.Apis.Auth.OAuth2
Nuget package plus whatever other service-specific package you plan on using the credential with (in my case it isGoogle.Apis.Drive.v3
).