【问题标题】:Get UserCredential of Google Drive API using Refresh token, ClientId and ClientSecret in ASP.net/C#在 ASP.net/C# 中使用 Refresh token、ClientId 和 ClientSecret 获取 Google Drive API 的 UserCredential
【发布时间】:2014-11-25 07:15:58
【问题描述】:

按照此链接中给出的说明,我能够获得 RefreshToken:How do I authorise a background web app without user intervention? (canonical ?)

现在我需要user credential 来获取驱动服务object。我搜索了很多,但找不到确切的解决方案。

我可以使用刷新令牌获取访问令牌,ClientIdClientSecret 按照此链接:Using OAuth 2.0 for Web Server Applications - offline

但在那之后我怎样才能使用AccessToken 获得user credential?或者使用刷新令牌获取credential 的任何直接方式。任何示例或建议。

谢谢!

【问题讨论】:

    标签: c# asp.net google-drive-api google-oauth google-api-dotnet-client


    【解决方案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# 教程。

    【讨论】:

    • 我不想使用 %appData% 或任何其他存储。是否可以使用 Refresh tokenClientIdClientSecret 获取 UserCredential ?
    • 您可以创建自己的 IDatastore 实现,并按照您的喜好进行操作。或者您可以通过使用客户端库并手动完成所有操作daimto.com/google-3-legged-oauth2-flow
    • 绕过过程的任何 c# 示例?
    • 如果您检查github.com/LindaLawton/Google-Dotnet-Samples/blob/master/…,您将看到如何创建一个将其存储在数据库中的数据库,您应该能够对其进行编辑以发送刷新令牌。在这里发布它太大了。
    猜你喜欢
    • 2016-08-30
    • 2014-08-05
    • 2014-09-03
    • 2020-05-26
    • 2016-12-15
    • 2019-06-16
    • 2017-09-01
    • 2022-01-04
    • 2013-06-03
    相关资源
    最近更新 更多