23

Sto utilizzando "Google.Apis.Bigquery.v2 Libreria client" con C#.È possibile utilizzare la chiave json invece del tasto p12 per le credenziali dell'account di servizio?

Autorizzo Google BigQuery utilizzando "Account servizio" (vedere http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html). Per creare il certificato X509, utilizzo la chiave p12 dalla Google Developers Console. Tuttavia, in questo momento la chiave JSON è l'impostazione predefinita. Posso usarlo invece il tasto p12?

Ho il codice seguente:

string serviceAccountEmail = "[email protected]"; 

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(); 
+0

La domanda simile (senza risposta) è qui http://stackoverflow.com/questions/30884184 –

+0

Hai provato a utilizzare questo flusso: http://stackoverflow.com/questions/19977864/nativeapplicationclient-is-not-supported-any ma per BQ? – Ryan

+0

Questo è in realtà molto vicino. Hai un esempio simile per ServiceAccountCredential (non UserCredential)? –

risposta

10

E 'ora possibile (ho usato v 1.13.1.0 del Google API).

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

Esempio per Google Fogli:

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

Questo è il lavoro per me:

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

Ovviamente, si dovrebbero fornire i propri valori per la Variabile scopes e per il percorso del file chiave. Dovresti anche prendere il pacchetto Nuget Google.Apis.Auth.OAuth2 più qualsiasi altro pacchetto specifico del servizio che desideri utilizzare (con il mio caso è Google.Apis.Drive.v3).