我一直堅持這個好幾天了。我複製了google api示例中的確切代碼,以將文件上傳到Google雲端硬盤。下面是代碼找不到方法:'虛空Google.Apis.Util.Store.FileDataStore..ctor(System.String)'
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = ClientId,
ClientSecret = ClientSecret,
},
new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile },
"user",
CancellationToken.None,
new FileDataStore("MyStore")).Result;
但它會拋出運行時異常:未找到方法:「太虛Google.Apis.Util.Store.FileDataStore..ctor(System.String)」。我已經添加了必要的Google Api dll。
或者,如果任何人都可以建議一個更好的代碼上傳文件到谷歌驅動器在實現服務器端授權的網站。任何幫助將不勝感激。
更新:我改變了我的代碼,這
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);
但它也會引發異常:找不到方法:「太虛Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow..ctor(初始化器)。 dll的問題是什麼?
這是在運行時拋出一個異常,還是你得到一個編譯錯誤? –
在運行時拋出 – user3732193
我不知道爲什麼你有這個異常,但我認爲你應該至少有這樣的事情:UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(...);或者如果你不想執行你的方法異步類似的東西:var task = GoogleWebAuthorizationBroker.AuthorizeAsync(...); var cred = task.Wait();你能試一下嗎 ?順便說一句,不要執行異步不是一件好事,因爲它會阻止你的應用程序的主線程。 – Dragouf