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

相关问题