【问题标题】:AssertionFlowClient depreceated, trying to use ServiceAccountCredential, but it won't work不推荐使用 AssertionFlowClient,尝试使用 ServiceAccountCredential,但它不起作用
【发布时间】:2013-10-29 19:11:11
【问题描述】:

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

我已经从谷歌文档中复制了这段代码https://developers.google.com/drive/delegation

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

但我在尝试构建项目时收到此警告:

警告 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”

然后我用谷歌搜索了 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;
    }

当我尝试构建此代码时,它似乎一切正常,但当我运行它时,我收到以下错误。

在 mscorlib.dll 中发生了“System.Security.Cryptography.CryptographicException”类型的第一次机会异常

附加信息:Det går inte att hitta det begärda objektet。 (翻译:找不到请求的对象)

如果有这个异常的处理程序,程序可以安全地继续。

错误发生在这一行:

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

有人有什么想法吗?

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();

我收到此错误消息:

Google.Apis.dll 中出现“Google.Apis.Auth.OAuth2.Responses.TokenResponseException”类型的未处理异常

附加信息:错误:“access_denied”,描述:“”,Uri:“”

【问题讨论】:

    标签: google-drive-api google-api-dotnet-client


    【解决方案1】:

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

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

    【讨论】:

    • 抱歉,不行!检查路径并且所有 oki (我把它放在 C:\temp\key.p12 中)当我将行更改为: X509Certificate2 certificate = new X509Certificate2(@"C:\temp\key.p12", "notasecret", X509KeyStorageFlags.Exportable);发生同样的错误:如果我将路径更改为例如 @"C:\tamp\key.p12",我会收到另一条错误消息:在 mscorlib.dll 中发生“System.Security.Cryptography.CryptographicException”类型的第一次机会异常附加信息:Det går inte att hitta sökvägen。 (翻译:“找不到路径”)
    • 我仍然无法让它工作。会不会是某种版本不匹配?此外,您在答案中提供的链接使用 AssertionFlowClient,但正如我在问题中提到的那样,我收到一个构建警告,它不再受支持。我错过了什么吗?
    • 对不起,试试这个链接 - code.google.com/p/google-api-dotnet-client/source/browse/…。您能否按照 README 文件验证此示例是否按预期工作?而且..只是为了验证 - 您是否将从 Google API 控制台下载的文件复制到此位置 (C:\Temp\key.p12)?
    • 我找到了最后一个错误的答案。在管理员控制台/安全/高级设置/3d 方 Oauth2 中,我应该添加服务帐户 ID 和范围。在新的 Cloud Console 中很难找到这个 ID,但我打开了旧的 APIs COnsole 并找到了它。
    • @peleyal 在新的 Cloud Console 中究竟哪里可以找到客户端 ID?在检查了旧的 Api 控制台布局后,我看到了大量在新外观中找不到的信息,包括客户端 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2019-12-17
    相关资源
    最近更新 更多