C#Google Drive API错误“Google.Apis.Services.BaseClientService.Initializer”不包含“Authenticator”的定义

时间:2014-06-12 07:50:22

标签: c# google-drive-api

我正在尝试使用域范围授权的示例代码。我正在实现此链接中指定的代码:https://developers.google.com/drive/web/delegation

    private DriveService GetService(String userEmail)
    {

        X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
        {
            ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
            Scope = DriveService.Scope.Drive.GetStringValue(),
            ServiceAccountUser = userEmail,
        };

        authenticator = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);


        return new DriveService(new BaseClientService.Initializer() { Authenticator = authenticator, ApplicationName = "sample" });

    }

当我编译它时,我收到一条错误,说“'Google.Apis.Services.BaseClientService.Initializer'不包含'Authenticator'的定义”。我认为这是因为最近对API进行了更改。那么有人可以建议如何克服这个错误吗?

1 个答案:

答案 0 :(得分:1)

我想建议您使用Google dot net客户端库。

NuGet命令

PM> Install-Package Google.Apis.Drive.v2 

示例代码:

using Google.Apis.Auth.OAuth2;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Services;
using Google.Apis.Drive.v2;  

var serviceAccountEmail = "539621478859-imkdv94bgujcom228h3ea33kmkoefhil@developer.gserviceaccount.com";
ServiceAccountCredential certificate = new X509Certificate2(@"C:\dev\GoogleDriveServiceAccount\key.p12", "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               Scopes = new[] { DriveService.Scope.DriveReadonly }
           }.FromCertificate(certificate));

// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

然后通过服务运行您的所有请求。如果您尚未确定在开发人员控制台应用程序应用程序中同时选择了Drive API and Drive SDK