Google drive api AssertionFlowClient已弃用,试图使用ServiceAccountCredential,但它赢了';行不通

Google drive api AssertionFlowClient已弃用,试图使用ServiceAccountCredential,但它赢了';行不通,google-drive-api,google-api-dotnet-client,Google Drive Api,Google Api Dotnet Client,我已尝试使用服务帐户代表其他用户创建DriveService 我从这里找到的谷歌文档中复制了这段代码 当我试图构建这段代码时,它看起来很好,但是当我运行它时,我得到了以下错误 mscorlib.dll中发生类型为“System.Security.Cryptography.CryptographyException”的首次意外异常 其他信息:Det går inte att Det begärda objektet。 (已翻译:找不到请求的对象) 如果存在此异常的处理程序,则程序可以安全地继续 错

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

我从这里找到的谷歌文档中复制了这段代码

当我试图构建这段代码时,它看起来很好,但是当我运行它时,我得到了以下错误

mscorlib.dll中发生类型为“System.Security.Cryptography.CryptographyException”的首次意外异常

其他信息:Det går inte att 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();
{
Console.WriteLine(“驱动API-服务帐户”);
Console.WriteLine(“========================================”);
字符串serviceAccountEmail=“@developer.gserviceaccount.com”;
var证书=新的X509Certificate2(@“key.p12”,“notasecret”,X509keystrageFlags.Exportable);
ServiceAccountCredential credential=新ServiceAccountCredential(
新ServiceAccountCredential.初始值设定项(serviceAccountEmail)
{
User=“@.mygbiz.com”,
Scopes=new[]{DriveService.Scope.Drive}
}.FromCertificate(证书));
//创建服务。
var service=new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=“DrvMgr”,
});
Console.WriteLine(“执行上市”);
FileList UserFiles=service.Files.List().Execute();
我收到以下错误消息:

Google.api.dll中发生类型为“Google.api.Auth.OAuth2.Responses.TokenResponseException”的未处理异常


其他信息:错误:“访问被拒绝”,说明:“,Uri:”

您的p12文件路径似乎不正确。有关有效的解决方案,请参阅示例


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

对不起,没有!路径被选中,所有OK(我将其放在C:\temp\key.p12中)当我将行更改为:X509Certificate2 certificate=new X509Certificate2(@“C:\temp\key.p12”,“notasecret”,X509keystargeFlags.Exportable);同样的错误也会发生:如果我将路径更改为例如@“C:\tamp\key.p12”我收到另一条错误消息,上面写着:mscorlib.dll中发生了“System.Security.Cryptography.CryptographyException”类型的第一次意外异常。其他信息:Det går inte att hitta sökvägen(翻译为:“找不到路径”)我仍然无法让它工作。可能是某种版本不匹配吗?此外,您在回答中提供的链接使用了AssertionFlowClient,但正如我在问题中提到的,我收到了一个构建警告,表示不再支持它。我是否缺少什么内容?抱歉,请尝试此链接-。您是否可以按照自述文件验证此示例是否可以作为ex期待?和..只是为了验证-您是否将从Google API控制台下载的文件复制到此位置(C:\Temp\key.p12)?我找到了上一个错误的答案。在Administrators console/Security/Advanced Settings/3d party Oauth2中,我应该添加服务帐户ID和作用域。在新的云控制台中很难找到该ID,但我打开了旧的API控制台并找到了它。@peleyal在新的云控制台中到底在哪里找到客户端ID?签出后我看到的旧Api控制台布局中有大量新外观中找不到的信息,包括客户端ID。
X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
{
        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();