断言AssertionFlowClient,尝试使用ServiceAccountCredential,但它不起作用

JoB*_*oBe 5 google-drive-api google-api-dotnet-client

我尝试使用代表其他用户的服务帐户创建DriveService.

我已经从https://developers.google.com/drive/delegation中的 Google文档中复制了此代码

    static DriveService BuildService() {
            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.Scopes.Drive.GetStringValue(),
        };
        var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
        return new DriveService(auth);
    }
Run Code Online (Sandbox Code Playgroud)

但是在尝试构建项目时我收到了这个警告:

警告4'Google.Apis.Authentication.OAuth2.DotNetOpenAuth.AssertionFlowClient'已过时:'不再支持AssertionFlowClient,它将在1.7.0-beta中删除.考虑使用新的Google.Apis.Auth NuGet包中的ServiceAccountCredential.

我也得到这个错误:

错误11参数1:无法从'Google.Apis.Authentication.OAuth2.OAuth2Authenticator'转换为'Google.Apis.Services.BaseClientService.Initializer'

然后我用Google搜索了ServiceAccountCredential并最终获得了此代码(来自此页面:https: //code.google.com/p/google-api-dotnet-client/wiki/OAuth2#Service_Accounts)

static DriveService BuildService() {
            X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
            {
                User = "someone@mydomain.mygbiz.com", 
                Scopes = new[] { DriveService.Scope.DriveFile }
            }.FromCertificate(certificate));

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

        return service;
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试构建这个代码时似乎一切都很好,但是当我运行它时,我得到以下错误.

mscorlib.dll中出现'System.Security.Cryptography.CryptographicException'类型的第一次机会异常

其他信息:Detgårinteatt hitta detbegärdaobjektet.(翻译:无法找到请求的对象)

如果存在此异常的处理程序,则可以安全地继续该程序.

此行发生错误:

X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)

有人有主意吗?

更新2013年10月31日我试过这段代码:

{
        Console.WriteLine("Drive API - Service Account");
        Console.WriteLine("==========================");

        String serviceAccountEmail = "<some email>@developer.gserviceaccount.com";

        var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               User = "<someuser>@<mydomain>.mygbiz.com",
               Scopes = new[] { DriveService.Scope.Drive }
           }.FromCertificate(certificate));

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

        Console.WriteLine("Executing listing");
        FileList UserFiles = service.Files.List().Execute();
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

Google.Apis.dll中发生未处理的"Google.Apis.Auth.OAuth2.Responses.TokenResponseException"类型异常

其他信息:错误:"access_denied",描述:"",Uri:""

pel*_*yal 0

您的 p12 文件的路径似乎不正确。请参阅Plus.ServiceAccount示例以获取可行的解决方案。

我认为在此示例中,key.p12 作为内容文件添加到项目中,该文件始终复制到输出目录。然后在代码中提及“key.p12”文件路径将导致从输出文件夹中获取该文件。