我在哪里可以找到ServiceAccountCredential

时间:2014-05-27 07:23:17

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

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

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

但我找不到ServiceAccountCredential。

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

3 个答案:

答案 0 :(得分:15)

以下是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
    }
    

答案 1 :(得分:10)

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


        }
    }
}

答案 2 :(得分:1)

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

相关问题