Estoy usando "Google.Apis.Bigquery.v2 Client Library" con C#.
Estoy autorizando a Google BigQuery usando "Cuenta de servicio" (consulte http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html ). Para crear el certificado X509, uso la clave p12 de Google Developers Console. Sin embargo, en este momento la clave json es la predeterminada. ¿Puedo usarla en lugar de la tecla p12?
Tengo el siguiente código:
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();Ahora es posible (utilicé v 1.13.1.0 de las API de Google).
Ejemplo para 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);Ejemplo para Hojas de cálculo de Google:
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;Obtuve el enlace, que indica que la autenticación de la cuenta de servicio mediante el archivo JSON en la aplicación C# aún no se ha agregado en la API de Google BigQuery .
https://github.com/google/google-api-dotnet-client/issues/533
Esto es trabajo para mí:
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; } Obviamente, proporcionaría sus propios valores para la scopes de ámbito y para la ruta al archivo de claves. También necesitaría obtener el paquete Google.Apis.Auth.OAuth2 Google.Apis.Auth.OAuth2 más cualquier otro paquete específico del servicio con el que planee usar la credencial (en mi caso, es Google.Apis.Drive.v3 ).