使用ASP.net/C#中的刷新令牌,ClientId和ClientSecret获取Google Drive API的UserCredential [英] Get UserCredential of Google Drive API using Refresh token, ClientId and ClientSecret in ASP.net/C#

查看:350
本文介绍了使用ASP.net/C#中的刷新令牌,ClientId和ClientSecret获取Google Drive API的UserCredential的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够按照此链接中给出的说明获取RefreshToken:

I was able to get a RefreshToken by following the instruction given in this link : How do I authorise a background web app without user intervention? (canonical ?).

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

Now I need user credential for getting driveservice object. I have searched a lot but cant find a exact solution.

通过此链接,我可以使用刷新令牌ClientIdClientSecret获取访问令牌:

I can get access token using refresh token, ClientId and ClientSecret following this link :Using OAuth 2.0 for Web Server Applications - offline.

但是在那之后如何使用AccessToken获取user credential?或使用刷新令牌获取credential的任何直接方法.任何例子或建议.

But after that how can I get user credential using that AccessToken? Or any direct way for getting credential using refresh token. Any example or suggestion.

谢谢!

推荐答案

您可能希望为此使用客户端库.您需要做的就是一次运行以下代码.刷新令牌将由FileDataStore创建并存储在您的%appData%目录中.下次运行时,它不会提示您进行身份验证,因为它将已经具有身份验证.您也可以创建自己的iDataStore实现,以将刷新令牌存储在任意位置.我喜欢当前目录 LocalFileDatastore.cs .

You might want to try using the Client Lib for this instead of that. All you need to do is run the following code once. The refresh token will be created by FileDataStore and stored in your %appData% directory. The next time it runs it wont prompt you for authentication because it will have it already. You could also create your own implementation of iDataStore to store the refresh token where ever you like. I like current directory 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云端硬盘API C#教程中摘录的代码.

Code ripped from Google Drive API C# tutorial.

这篇关于使用ASP.net/C#中的刷新令牌,ClientId和ClientSecret获取Google Drive API的UserCredential的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆