如何在.net Google API V3中刷新令牌? [英] How to refresh token in .net google api v3?

查看:97
本文介绍了如何在.net Google API V3中刷新令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在前端获取了access_token和refresh_token,现在将其存储在数据库中.在后端.netcore应用程序中,我使用以下方式创建了googlecredential:

I managed to get the access_token and refresh_token on front end and now its stored in a database, On the backend .netcore application I create googlecredential with

var cred=googleCredential.fromAccessToken(access_token);

但是这个不会自己刷新,我也不知道如何刷新.我可以将发布请求发送到他们的其余api并刷新它,但我需要知道何时"cred"已过期,无法发送该刷新请求.PS知道令牌位于数据库中,因此不能使用.fromaccesstoken以外的任何选项.

but this one doesn't refresh on its own and I have no idea how to refresh this. I can send a post request to their rest api and refresh it but I need to know when "creds" has expired to send that refresh request. PS know that the tokens are in a database so using anything but .fromaccesstoken is not an option.

完整的代码无法共享,但这是应用程序流程的进行方式.在reactJS应用程序上,我通过react-google-login模块获取授权代码

Complete code can not be shared but here's how the application's flow is going. On a reactJS application I get the authorization code via react-google-login module

<GoogleLogin
                  
                  clientId="xxx"
                  buttonText="Login"
                  onSuccess={this.responseGoogle}
                  onFailure={this.responseGoogle}
                  cookiePolicy={"single_host_origin"}
                  scope="https://www.googleapis.com/auth/drive.readonly"
                  accessType="offline"
                  prompt="consent"
                  responseType="code"
                />

这将返回一个授权码作为唯一的响应主体,然后将该响应与另一个端点一起使用以交换前端的刷新和访问令牌,这些令牌被发送到将其存储在db中的后端端点.现在,服务器端可以通过

this returns an authorization code as the only response body, this response is then used with another endpoint to exchange for refresh and access token at front end, these tokens are sent to a backend endpoint which stores them in a db. Now the server side can create a googlecredential object via

this.googleCredentials=GoogleCredential.FromAccessToken(this.googleAccessToken);

唯一的问题是,它不能单独处理刷新,因此您必须以某种方式实现它.

Only problem being this doesnt handle the refreshing on its own so you have to somehow implement it.

推荐答案

由于通过 fromtoken(ACCESS_TOKEN) GoogleCredentials 对象时,找不到其他刷新方法>我已经完成的工作是向后端的其余端点发送带有刷新令牌的发布请求,并获得一个新的access_token.面临的另一个问题是找出访问令牌何时过期,而找不到解决方案.但是在我的情况下,通过驱动器服务创建驱动器文件夹时返回401异常

Since I can't find another way to refresh when GoogleCredentials object is created via fromtoken(ACCESS_TOKEN) what I've done is sending a post request with the refresh token that I have to the rest endpoint at the backend and get a new access_token. Another problem faced was finding out when the access token was expired for which no solution could be found. But it returns a 401 exception at creating the drive folders via drive service in my case

var res =等待this.driveService.Files.Create(文件夹).ExecuteAsync()

此处 res 将引发异常,我将其放在 try 块中,并通过 catch 中的http请求获得了新的访问令牌.并再次运行该语句(提示: recursion )

here res will raise an exception and I just put it in a try block and got the new access token via http request in catch and ran the statement again (hint: recursion)

希望这对任何人都有帮助,但是会欢迎提出更好的解决方案.请理解,这将是一个从数据库读取refreshtoken的后端解决方案.

Hope this helps anyone, but better proposed solutions will be welcomed. Please understand that this is going to be a backend solution reading refreshtoken from database.

这篇关于如何在.net Google API V3中刷新令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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