我在哪里可以找到ServiceAccountCredential

我在使用asp.net c#的google api工作,我的目标是使用服务帐户访问google api。

我已导入所有需要的dll以创建服务帐户以访问管理员function(admin sdk)。

但我找不到ServiceAccountCredential。

我如何在我的项目中实现这一点?

以下是您2017年的表现:

  • 转到“ 服务帐户”页面
  • 为服务帐户创建一个私钥作为JSON文件
  • 将下载的文件放入项目中(用于开发)或在构建过程中烘焙它
  • 在您的代码中引用Google.Apis.Auth包

    using (var stream = new FileStream("key.json", FileMode.Open, FileAccess.Read)) { var credential = GoogleCredential.FromStream(stream) .CreateScoped(scopes) .UnderlyingCredential as ServiceAccountCredential; //profit } 

ServiceAccountCredential是Google.Apis.Auth.OAuth2的一部分

使用BigQuery的简单示例:

 using System; using Google.Apis.Auth.OAuth2; using System.Security.Cryptography.X509Certificates; using Google.Apis.Bigquery.v2; using Google.Apis.Services; //Install-Package Google.Apis.Bigquery.v2 namespace GoogleBigQueryServiceAccount { class Program { static void Main(string[] args) { Console.WriteLine("BigQuery API - Service Account"); Console.WriteLine("=========================="); String serviceAccountEmail = "539621478854-imkdv94bgujcom228h3ea33kmkoefhil@developer.gserviceaccount.com"; var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable); ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) { Scopes = new[] { BigqueryService.Scope.DevstorageReadOnly } }.FromCertificate(certificate)); // Create the service. var service = new BigqueryService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "BigQuery API Sample", }); } } } 

我正在使用Xamarin Studio,我使用ServiceAccountCredential在我的Mac上运行NUnit测试库项目(PCL)。 我只是试图将它移动到Android测试项目(MonoDroid),而ServiceAccountCredential不存在。 ServiceAccount确实存在(它的祖先类)。 因此,此问题可能与编译目标有关,并且没有为您的目标实现ServiceAccountCredential。