23

C# ile "Google.Apis.Bigquery.v2 İstemci Kitaplığı" kullanıyorum.Servis hesabı kimlik bilgileri için p12 anahtarı yerine json anahtarını kullanmak mümkün mü?

Google BigQuery'ye "Hizmet Hesabı" nı kullanarak izin veriyorum (bkz. http://www.afterlogic.com/mailbee-net/docs/OAuth2GoogleServiceAccounts.html). X509 sertifikasını oluşturmak için p12 anahtarını Google Developers Console'dan kullanıyorum. Ancak, şu anda json anahtar varsayılan değerdir. Bunun yerine p12 anahtarını kullanabilir miyim?

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

Benzer (yanıtlanmamış) soru şu anda buradadır: http://stackoverflow.com/questions/30884184 –

+0

Bu akışı kullanmayı denediniz: http://stackoverflow.com/questions/19977864/nativeapplicationclient-is-not-supported-any ama BQ için? – Ryan

+0

Bu aslında çok yakın. ServiceAccountCredential (UserCredential değil) için benzer bir örneğiniz var mı? –

cevap

10

(ı v tarihinde API'larından 1.13.1.0 kullanılır). BigQuery için

Örnek: Google E için

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

Örnek:

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

Bu benim için çalışıyor: Açıkçası

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

, için kendi değerlerini sağlayacak scopes değişken ve anahtar dosyasının yolu için. Ayrıca, Google.Apis.Auth.OAuth2 Nuget paketini ve kimlik bilgisini kullanmayı planladığınız hizmete özel başka herhangi bir paket olsun (benim durumumda Google.Apis.Drive.v3).

İlgili konular