如何刷新1小时后到期的Google服务帐户? [英] How to refresh Google Service account which expires after 1 hour?

查看:99
本文介绍了如何刷新1小时后到期的Google服务帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

https://developers.google.com/drive/delegation

我可以使用DriveService对象,但是1个小时后,它出现了以下错误:远程服务器返回了错误:(401)未经授权."

I am able to work with DriveService object, but after 1 hr, it errors out with exception: "The remote server returned an error: (401) Unauthorized."

我知道,通过将"access_type"设置为"offline",我们可以解决此问题,但是我无法为DriveService对象设置此属性.
有谁知道如何刷新此Google Drive服务对象?

I know, by setting "access_type" to "offline" we could solve this problem, but I am not able to set this property for DriveService object.
Does anyone know how to refresh this Google Drive Service object?

预先感谢

推荐答案

设置脱机访问模式时,当用户首次登录时,您的应用将获得刷新令牌.

When you set offline access mode, your app gets a refresh token when the user logs in for the first time.

access_type :: 指示您的应用程序在 用户不在浏览器中.该参数默认为在线. 如果您的应用程序需要在用户处于访问状态时刷新访问令牌 在浏览器中不存在,然后离线使用.这将导致您 您的应用程序第一次获得刷新令牌的应用程序 交换用户的授权码.

access_type :: Indicates if your application needs to access a Google API when the user is not present at the browser. This parameter defaults to online. If your application needs to refresh access tokens when the user is not present at the browser, then use offline. This will result in your application obtaining a refresh token the first time your application exchanges an authorization code for a user.

当前访问令牌过期后,您稍后可以使用此刷新令牌来获取新的访问令牌.基本上,您的应用将随后到达令牌交换端点(POST到 https://accounts.google.com /o/oauth2/token )以及刷新令牌和您的客户端凭据-谷歌随后向您发出(刷新令牌+访问令牌)对.
有关进一步的说明,请参见此链接.

You later use this refresh token to obtain a new access token, once the current access token expires. Basically, your app would then hit the token exchange endpoint (POST to https://accounts.google.com/o/oauth2/token) with the refresh token and your client credentials - google with then issue a (refresh token + access token) pair to you.
See this link for further clarification.

编辑-
我检查了服务帐户文档,并找到了一个示例C#应用程序,该应用程序也可以获取并使用刷新令牌.在此处查看.
我希望这个对您有所帮助.

EDIT -
I checked the Service Account documentation and found a sample C# app that fetches and uses refresh tokens too. See it here.
I hope this one helps.

这篇关于如何刷新1小时后到期的Google服务帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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