4

我一直堅持這個好幾天了。我複製了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的問題是什麼?

+0

這是在運行時拋出一個異常,還是你得到一個編譯錯誤? –

+0

在運行時拋出 – user3732193

+0

我不知道爲什麼你有這個異常,但我認爲你應該至少有這樣的事情:UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(...);或者如果你不想執行你的方法異步類似的東西:var task = GoogleWebAuthorizationBroker.AuthorizeAsync(...); var cred = task.Wait();你能試一下嗎 ?順便說一句,不要執行異步不是一件好事,因爲它會阻止你的應用程序的主線程。 – Dragouf

回答

1

看起來像v1.8.2 of Google APIs Client Library中有一個錯誤。嘗試使用與NET框架4 v1.8.1 using nuget

+0

謝謝!這也讓我發瘋,但降級工作。我希望這一切都能儘快解決。 – squid808

+0

squid808m,user3732193和Antony Thomas, – peleyal

1

這真的很奇怪。我們所有的樣品都使用新庫(1.8.2),並且它們都可以工作。也許它與VS2010有關,或者不安裝最新的.NET 4補丁之一? (確保的.NET Framework 4.0更新KB2468871(http://www.microsoft.com/en-us/download/details.aspx?id=3556)安裝。

該樣品成功運行意味着我們的環境是不同的事實。我使用VS 2012的Windows 8。你用什麼?

隨意在我們的問題跟蹤(https://code.google.com/p/google-api-dotnet-client/issues/list)打開一個新的問題,我們可以從那裏

1

這是我做了什麼來解決這個問題:

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; 
} 
+0

您使用的是什麼.NET版本? – Hooch

0

有同樣的問題使用Visual St udio 2013 +更新3贏得8.1 .NET 4.5被設置爲我的控制檯項目的目標框架(這是默認選擇的)。 該問題已得到解決,取消了對每個谷歌apis的引用。 然後切換目標框架爲」 .NET框架4" (從解決方案資源管理器中選擇項目,右鍵 - >屬性 - >應用程序頁面 - >目標框架下拉列表) 在下列順序的NuGet然後安裝的API:

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

要壞我需要.NET 4.5的功能。 :( – Hooch

相關問題