我尝试按照这里的说明操作:
https://code.google.com/p/google-api-dotnet-client/wiki/Build
Run Code Online (Sandbox Code Playgroud)
但是当我尝试遵循"使用NuGet"下的说明时,我失败了!
我收到以下回复:
Attempting to resolve dependency 'Newtonsoft.Json (? 5.0.5)'.
Attempting to resolve dependency 'Microsoft.Bcl (? 1.0.19)'.
Attempting to resolve dependency 'Microsoft.Bcl.Build (? 1.0.4)'.
Attempting to resolve dependency 'Microsoft.Bcl.Async (? 1.0.16)'.
Attempting to resolve dependency 'Microsoft.Net.Http (? 2.1.10)'.
Attempting to resolve dependency 'Zlib.Portable (? 1.9.2)'.
Attempting to resolve dependency 'log4net (? 2.0.0)'.
Successfully installed 'Newtonsoft.Json 5.0.6'.
Successfully installed 'Microsoft.Bcl.Build 1.0.7'.
Successfully installed 'Microsoft.Bcl 1.0.19'.
Successfully installed 'Microsoft.Bcl.Async 1.0.16'.
Successfully installed 'Microsoft.Net.Http 2.1.10'.
Successfully installed 'Zlib.Portable 1.9.2'.
Successfully installed 'log4net …Run Code Online (Sandbox Code Playgroud) 我尝试使用代表其他用户的服务帐户创建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 …Run Code Online (Sandbox Code Playgroud)