2013-06-27 63 views
1

我使用C#/ .NET从Perform Google Apps Domain-wide Delegation of Authority中尝试示例代码,并且像我在其他示例中尝试过的那样,创建使用auth变量的对象的代码部分表示:他们的语法是错误的。下面是我的代码有:使用OAuth2对Google API进行身份验证

using System; 
using System.Security.Cryptography; 
using System.Security.Cryptography.X509Certificates; 
using DotNetOpenAuth.OAuth2; 
using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using Google.Apis.Drive.v2; 
using Google.Apis.Util; 

namespace GoogleAPIDemo 
{ 
    class DriveServiceObject 
    { 
     private const string SERVICE_ACCOUNT_EMAIL = "<some-id>@developer.gserviceaccount.com"; 
     private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\path\to\<public_key_fingerprint>-privatekey.p12"; 

     /// <summary> 
     /// Build a Drive service object authorized with the service account 
     /// that acts on behalf of the given user. 
     /// </summary> 
     /// @param userEmail The email of the user. 
     /// <returns>Drive service object.</returns> 
     static DriveService BuildService(String userEmail) 
     { 
      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(), 
       ServiceAccountUser = userEmail, 
      }; 
      var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState); 

      return new DriveService(auth); 
     } 
    } 
} 

,我看到的是

(local variable) OAuth2Athenticator<AssertionFlowClient> auth 

Error: 
    The best overloaded method match for 'Google.Apis.DriveService.DriveService(Googel.Apis.Services.BaseClientService.Initializer)' has some invalid arguments 

这是我第一次编写使用谷歌的API和得到这个工作的任何帮助,一个应用程序将不胜感激的错误!

+0

什么是''Google.Apis.DriveService.DriveService'预期的参数,因为它不是一个'OAuth2Authenticator

回答

3

这一个工程:

 var provider = new AssertionFlowClient(
      GoogleAuthenticationServer.Description, 
      new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable)) 
     { 
      ServiceAccountId = serviceAccountEmail, 
      Scope = DriveService.Scopes.Drive.GetStringValue(), 
      ServiceAccountUser = driveHolderAccountEmail 
     }; 
     var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState); 

     m_service = new DriveService(new BaseClientService.Initializer() 
     { 
      Authenticator = auth 
     }); 
+0

立即试用此...将很快发布。谢谢! – GeneBean

+0

这解决了我的问题。 – GeneBean

+0

什么是m_service和keyPassword在这里?它给出的错误(名称'm_service'在当前上下文中不存在)。请帮助。 –

相关问题