Google身份服务:如何保存会话
我正在迁移一个与Google Drive JS API交互的Web应用程序,并迁移到新的Google Identity Services API,并遵循此 QuickStart Guide 。 GIS是强制性的,因为从2023年3月开始,旧的将不再使用。
在本指南中,只有一个小便条提及保留页面上的状态之后的记录:
注意:在初始用户授权之后,您可以立即致电gapi.auth.auth.authorize。
但是,没有明确的代码示例如何做到这一点,此外,人们可以在迁移指南,gapi.auth2.authorize()
已弃用。
- 使用一台TAP(一个带有ID“ G_ID_ONLOAD”的DIV)不是解决方案,因为我需要一个扩展范围(以稍后在Google Drive上访问)
- 将访问令牌存储在LocalStorage中(如某些线程中所述)是没有选项的,因为它违反了oauth模型
- 调用
requestAccessToken()
在每个页面重新加载而无需用户互动之后,这不是一个选项,因为第一个根本没有显示弹出窗口(在所有主要浏览器中被阻止),如果允许弹出弹出显示并立即隐藏(糟糕的UI)
可以给我一个例子,其中通过JS使用GSI,可以通过Page Reloads保存会话?
看来Google身份服务尚未准备就绪,还是我错了?
I'm migrating a web app that interacts with the google drive js api to the new Google Identity Services API and following this quickstart guide. GIS is mandatory, since the old one will no longer be in use from March 2023.
In this guide, there is only one small note mentionning to preserve the logged in state after page reload:
Note: After the initial user authorization, you can call gapi.auth.authorize with immediate:true to obtain an auth token without user interaction.
However, there's no clear code example how to do that, furthermore one can find in the migration guide, that gapi.auth2.authorize()
is deprecated.
- Using One Tap (a div with the id "g_id_onload") is not a solution, because I need an extended scope (to access later on google drive)
- Storing the access token in localstorage (as mentionned in some threads) is no option, since it violates the oauth model
- Calling
requestAccessToken()
after every page reload without user interaction is not an option, because 1st the popup is not shown at all (blocked in all major browsers) and 2nd if allowed the popup is shown and hiding immediately (bad ui)
Can somebody give me an example where GSI is used via JS that preserves sessions through page reloads?
It seems that Google Identity Services is not yet production ready or am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了提供帮助:
解决方案
如Sam所述:“您可以以某种方式保存访问令牌,并在重新加载后加快它来加速事物。”
给定 Google的Exampe ,我们应该应该调用
inittokencLient
为了配置Google auth和requestAcccessToken
以弹出auth:在您的
tokencallback
中,您可以保存以某种方式保存凭据,例如:最后,当您重新启动/重新加载应用程序并初始化
gapi.server
再次初始化时,您只需要再次获得凭据并将令牌设置为gapi
,例如:这样做,您的应用程序将在重新加载后起作用。我知道这不是最好的解决方案,但是看到您拥有的内容和图书馆提供的内容,我认为您可以做到。
ps:令牌在1小时后到期,并且没有刷新令牌(使用隐式流),因此,您必须要求用户再次登录。
In order to help:
Solution
As Sam described: "you can somehow save access token and use it to speed-up things after page reload."
Given the the Google's exampe, we should call
initTokenClient
in order to configure the Google Auth and therequestAccessToken
to popup the auth:In your
tokenCallback
you can save the credentials you get somehow, e.g.:Finally, when you restart/reload your application and you initialize the
gapi.server
again, you only need to get the credentials again and set token togapi
, like:Doing it, your application will work after the reload. I know it could not be the best solution, but seeing what you have and the library offers, I think that's you can do.
p.s.: the token expires after 1 hour and there is no refresh token (using the implicit flow) so, you will have to ask the user to sign-in again.