c# - 任务完成后如何销毁谷歌驱动器 token ?

标签 c# google-oauth google-api-dotnet-client google-drive-api

我能够使用身份验证后获得的凭据对 google drive 进行身份验证和执行操作,但此 token 存在很长时间。它说 expires-in : 3600 所以它应该在 1 小时内过期但是当我在一个月后尝试它时它使用该 token 并且它有效。

我的要求是在身份验证之后,无论正在执行的任务是否完成,如果用户再次启动程序,它应该再次要求用户进行身份验证。所以基本上我不想将 token 存储在客户的系统中。 Expires-in 对我不起作用,因为 token 会刷新并且不会再次要求身份验证。

下面是我正在使用的代码:

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                   new ClientSecrets
                   {
                       ClientId = "<myid>.apps.googleusercontent.com",
                       ClientSecret = "<Mysecret>"
                   },
                   new[] { DriveService.Scope.Drive },
                   "user",
                   CancellationToken.None).Result;

                // Create the service using the client credentials.
                DriveService service = new DriveService(new BaseClientService.Initializer()
                     {
                         HttpClientInitializer = credential,
                         ApplicationName = "sampleapp"
                     });
*****some task performed*********

现在在这个“一些任务”之后我想要销毁 token 。

请帮忙。

最佳答案

从今天早些时候发布的 1.8.2 版 (http://google-api-dotnet-client.blogspot.com/2014/05/announcing-release-of-182.html) 开始,我们支持 token 撤销。它撤销 token 并将其从数据存储中删除。

您只需执行以下操作:

await credential.RevokeTokenAsync(CancellationToken.None);

就这么简单。

更新: 在这篇博文中阅读有关 .NET 的 Google API 客户端库中 token 撤销的更多信息:http://peleyal.blogspot.com/2014/06/182-is-here.html

关于c# - 任务完成后如何销毁谷歌驱动器 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874990/

相关文章:

c# - 无法检索文件夹值

c# - 覆盖时保留注释

c# - 我究竟做错了什么?具有数据绑定(bind)的自定义用户控件内的依赖属性

C# Mongodb。分组后将 Json 投影为强类型

oauth-2.0 - Perl 中的 google API 服务帐户流程

python - 尝试使用 remote_api 连接时获取 'Refreshing due to a 401'

oauth-2.0 - 如何从服务帐户获取刷新 token

c# - 使用 C# 使用 Google 数据 API 访问 Google 电子表格

c# - 代码注释 : Do you put your code comments on Interfaces or on Concrete classes, 或两者?

c# - 如何在 c# 中使用 google apis 将邮件设置为 gmail 的 'Important'?