【问题标题】:where can i find ServiceAccountCredential我在哪里可以找到 ServiceAccountCredential
【发布时间】:2014-07-16 00:14:01
【问题描述】:

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

我已导入所有需要的 dll 来创建 service account 以访问管理功能(管理 sdk)。

但我找不到 ServiceAccountCredential。

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

【问题讨论】:

  • 您试过并得到解决方案了吗?
  • 肯定有一些文档断开连接。我也在想办法解决这个问题。当我弄清楚时,我会尽量记住回到这里。

标签: c# asp.net google-api google-oauth google-api-dotnet-client


【解决方案1】:

您在 2017 年的表现如下:

  • 转到Service Accounts page

  • 为服务帐号创建一个 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
      }
    

【讨论】:

  • 我收到“从 JSON 创建凭据时出错。无法识别的凭据类型。”知道为什么吗?
  • 我已经找到了解决问题的方法,但和你的不一样:stackoverflow.com/questions/41905074/…你怎么看?
【解决方案2】:

我正在使用 Xamarin Studio,并且我有一个使用 ServiceAccountCredential 在我的 Mac 上运行的 NUnit 测试库项目 (PCL)。我只是尝试将其移至 Android 测试项目(MonoDroid),并且 ServiceAccountCredential 不存在。 ServiceAccount 确实存在(它的祖先类)。所以这个问题可能与编译目标有关,并且没有为您的目标实现 ServiceAccountCredential。

【讨论】:

    【解决方案3】:

    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",
                });
    
    
            }
        }
    }
    

    【讨论】:

    • 我意识到这是一个旧答案,但这些部分来自哪里:new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
    • 谷歌开发者控制台你可以下载一个 p12 文件或者现在一个 json 文件,但是使用不同的代码
    猜你喜欢
    • 2012-05-30
    • 2013-07-13
    • 2012-06-05
    • 2011-04-24
    • 2021-02-02
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多