首页 > 解决方案 > 如何使用 chrome.identity.launchWebAuthFlow 缓存身份验证令牌?

问题描述

我正在使用chrome.identity.launchWebAuthFlow方法从我们的 OAuth2 服务器启动 OAuth2 令牌生成。

在 Identity API 文档中,指定要对用户进行身份验证,有两种可用方法:getAuthTokenlaunchWebAuthFlow. 我们使用后者,因为如文档中所述:

如果您的应用程序使用自己的服务器对用户进行身份验证,您将需要使用 launchWebAuthFlow。

我们正在努力缓存结果。在阅读 的文档时getAuthToken,似乎涉及到一些智能缓存。

Chrome 捕获响应并将 and 存储access_tokenrenew_token浏览器中(在此处可见:)chrome://identity-internals/当过期时,Chromeaccess_token才会向 OAuth 服务器请求。access_token

但是,使用launchWebAuthFlow时,我们很难使用 Chrome 缓存,并且它每次都会调用我们的 OAuth 服务器。

有谁知道如何使用 API 的这种方法缓存结果?

标签: google-chrome-extensiongoogle-chrome-app

解决方案


getAuthToken 仅指使用 Google 自己的 api 进行身份验证,如果您想自己做类似的事情,则必须自己实现。

只需使用 chrome.storage 来存储传入的凭据,并在使用它们时,然后仅在经过足够的时间后才触发 launchWebAuthFlow,否则无论如何都进行 fetch 调用,如果它返回未经身份验证,则刷新您的令牌


推荐阅读