找不到方法:’Void Google.Apis.Util.Store.FileDataStore..ctor(System.String)’

我已经被困在这几天了。 我复制了google api示例中的确切代码,以便将文件上传到Google云端硬盘。 这是代码

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = ClientId, ClientSecret = ClientSecret, }, new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile }, "user", CancellationToken.None, new FileDataStore("MyStore")).Result; 

但它会在运行时抛出一个exception:找不到方法:’Void Google.Apis.Util.Store.FileDataStore..ctor(System.String)’。 我已经添加了必要的Google Api dll。

或者,如果有人可以在实施服务器端授权的网站上建议更好的代码将文件上传到Google云端硬盘。 任何帮助将不胜感激。

更新:我将我的代码更改为此

 var token = new TokenResponse { RefreshToken = "1/6hnki1x0xOMU4tr5YXNsLgutzbTcRK1M-QOTEuRVxL4" }; var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = ClientId, ClientSecret = ClientSecret }, Scopes = new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile } }), "user", token); 

但它也引发了一个例外:未找到方法:’Void Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow..ctor(Initializer)。 是dll的问题?

看起来v1.8.2 of Google APIs Client Library存在错误。 尝试使用带有.NET framework 4的nuget的v1.8.1 。

这真的很奇怪。 我们所有的样本都使用新的库(1.8.2),它们可以工作。 也许它与VS2010有关,或者没有安装最新的.NET 4补丁之一? (确保已安装.NET Framework 4.0更新KB2468871( http://www.microsoft.com/en-us/download/details.aspx?id=3556 )。

样本成功运行的事实意味着我们的环境不同。 我在Windows 8上使用VS 2012.你用什么?

您可以在我们的问题跟踪器( https://code.google.com/p/google-api-dotnet-client/issues/list )中开启新问题,我们可以从那里开始使用。

这就是我为解决这个问题所做的工作:

 public UserCredential GetRefreshToken(string refreshToken, string clientID, string clientSecret, string[] scopes) { TokenResponse token = new TokenResponse { RefreshToken = refreshToken }; IAuthorizationCodeFlow flow = new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer(Google.Apis.Auth.OAuth2.GoogleAuthConsts.AuthorizationUrl, Google.Apis.Auth.OAuth2.GoogleAuthConsts.TokenUrl) { ClientSecrets = new ClientSecrets { ClientId = clientID, ClientSecret = clientSecret }, Scopes = scopes }); UserCredential credential = new UserCredential(flow, "me", token); try { bool success = credential.RefreshTokenAsync(CancellationToken.None).Result; } catch { throw; } return credential; } 

如果在win 8.1 .NET 4.5上使用Visual Studio 2013 + Update 3的相同问题被设置为我的控制台项目的目标框架(默认情况下已选中)。 通过删除对google apis的每个引用来解决此问题。 然后将目标框架切换到“.NET Framework 4”(从解决方案资源管理器中选择项目,右键单击 – >属性 – >应用程序页面 – >目标框架下拉列表)然后按以下顺序安装带有nuget的apis:

 PM> Install-Package Google.Apis PM> Install-Package Google.Apis.Auth PM> Install-Package Google.Apis.Drive.v2