0

我可以按照此鏈接中給出的說明獲得RefreshToken:How do I authorise a background web app without user intervention? (canonical ?)使用ASP.net/C中的Refresh令牌,ClientId和ClientSecret獲取Google Drive API的UserCredential

現在我需要user credential獲取driveservice object。我搜查了很多,但無法找到確切的解決方案。

我可以使用刷新令牌獲取訪問令牌,ClientIdClientSecret以下鏈接:Using OAuth 2.0 for Web Server Applications - offline

但之後,我怎麼能得到user credential使用AccessToken?或者使用刷新令牌獲得credential的任何直接方式。任何示例或建議。

謝謝!

回答

1

你可能想嘗試使用客戶端庫而不是那個。您只需運行一次以下代碼即可。刷新令牌將由FileDataStore創建並存儲在您的%appData%目錄中。下一次運行它不會提示您進行身份驗證,因爲它已經有了。您也可以創建自己的實現iDataStore來將刷新標記存儲在任何你喜歡的地方。我喜歡當前目錄LocalFileDatastore.cs

//Scopes for use with the Google Drive API 
string[] scopes = new string[] { DriveService.Scope.Drive, 
           DriveService.Scope.DriveFile}; 
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% 
UserCredential credential = 
      GoogleWebAuthorizationBroker 
          .AuthorizeAsync(new ClientSecrets { ClientId = CLIENT_ID 
                  , ClientSecret = CLIENT_SECRET } 
              ,scopes 
              ,"SingleUser" 
              ,CancellationToken.None 
              ,new FileDataStore("Daimto.GoogleDrive.Auth.Store") 
             ).Result; 

Google Drive API C#教程中翻譯的代碼。

+0

我不想使用%appData%或任何其他存儲。是否有可能使用**刷新令牌**,** ClientId **和** ClientSecret **獲取UserCredential? – hasnayn 2014-11-25 11:40:16

+0

您可以創建您自己的IDatastore實現,它可以滿足您的喜好。或者你可以通過使用客戶端庫通過手動完成所有手動http://www.daimto.com/google-3-legged-oauth2-flow/ – DaImTo 2014-11-25 11:41:13

+0

任何C#示例爲通過過程? – hasnayn 2014-11-25 11:49:10

相關問題