C#Google云端硬盘API错误“ Google.Apis.Services.BaseClientService.Initializer'不包含'Authenticator'的定义”

卡尔提克·拉梅什(Karthik Ramesh)

我正在尝试使用具有域范围的授权的示例代码。我正在实现此链接中指定的代码: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进行了更改。那么有人可以建议如何克服这个错误吗?

戴姆敦

我建议您使用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在开发人员控制台应用程序应用程序中同时选择了两者

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档