我已经尝试使用服务帐户代表另一个用户创建DriveService。AssertionFlowClient depreceated,试图使用ServiceAccountCredential,但它不会工作
我已经复制从谷歌文档,这个代码在这里找到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'
然后,我用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 = "[email protected]",
Scopes = new[] { DriveService.Scope.DriveFile }
}.FromCertificate(certificate));
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
return service;
}
当我尝试建立这个代码,似乎一切正常,但是当我运行它,我得到以下错误。
型 'System.Security.Cryptography.CryptographicException' 的第一次机会异常出现在mscorlib.dll
其他信息:雀鳝挪威INTE ATT hitta DETbegärdaobjektet。 (翻译:找不到请求的对象)
如果有这种异常的处理程序,程序可能会安全地继续。
错误发生在这条线:
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.Auth.OAuth2.Responses.TokenResponseException'发生在Google.Apis.dll
其他信息:错误: “ACCESS_DENIED”,说明: “” 乌里: “”
对不起,但没有!路径被检查并且所有的oki(我把它放在C:\ temp \ key中。p12) 当我将该行更改为: X509Certificate2 certificate = new X509Certificate2(@“C:\ temp \ key.p12”,“notasecret”,X509KeyStorageFlags.Exportable); 同样的错误发生: 如果我改变路径为例如@“C:\ tamp \ key.p12”我得到另一个错误消息说: 类型'System.Security.Cryptography.CryptographicException'的第一个机会异常发生在mscorlib.dll 其他信息:Detgårinte at at hittasökvägen。 (翻译:“无法找到路径”) – JoBe
我仍然无法使它工作。它可能是某种版本不匹配? 此外,您在答案中提供的链接使用AssertionFlowClient,但正如我在我的问题中提到的,我收到了一个构建警告,表示它不再受支持。我错过了什么吗? – JoBe
对不起,请尝试此链接 - https://code.google.com/p/google-api-dotnet-client/source/browse/Plus.ServiceAccount/?repo=samples。你能否遵循README文件并确认此示例按预期工作?而且..只是为了验证 - 是否将您从Google API Console下载的文件复制到此位置(C:\ Temp \ key.p12)? – peleyal