Google身份服务:如何保存会话

发布于 2025-02-04 05:13:14 字数 991 浏览 5 评论 0原文

我正在迁移一个与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2025-02-11 05:13:14

为了提供帮助:

  1. Google 3P授权Javascript javascript javascript库 in 我们可以检查新库具有的所有方法(它不会刷新令牌等。
  2. 此链接 GIS#cookies“ rel =“ nofollow noreferrer”>此文档说库不会控制cookie以保持状态。

解决方案

如Sam所述:“您可以以某种方式保存访问令牌,并在重新加载后加快它来加速事物。”

给定 Google的Exampe ,我们应该应该调用inittokencLient为了配置Google auth和requestAcccessToken以弹出auth:

tokenClient = google.accounts.oauth2.initTokenClient({
          client_id: 'YOUR_CLIENT_ID',
          scope: 'https://www.googleapis.com/auth/calendar.readonly',
          prompt: 'consent',
          callback: tokenCallback
      });
tokenClient.requestAccessToken({prompt: ''})

在您的tokencallback中,您可以保存以某种方式保存凭据,例如:

const tokenCallback(credentials) => {
    // save here the credentials using localStorage or cookies or whatever you want to.
}

最后,当您重新启动/重新加载应用程序并初始化gapi.server再次初始化时,您只需要再次获得凭据并将令牌设置为gapi,例如:

gapi.load('client', function() {
    gapi.client.init({}).then(function() {
      let credentials = // get your credentials from where you saved it
      credentials = JSON.parse(credentials); // parse it if you got it as string
      gapi.client.setToken(credentials);

      ... continue you app ...
    }).catch(function(err) {
      // do catch...
    });
});

这样做,您的应用程序将在重新加载后起作用。我知道这不是最好的解决方案,但是看到您拥有的内容和图书馆提供的内容,我认为您可以做到。

ps:令牌在1小时后到期,并且没有刷新令牌(使用隐式流),因此,您必须要求用户再次登录。

In order to help:

  1. Google 3P Authorization JavaScript Library: in this link we can check all the methods the new library has (it does not refresh token, etc..)
  2. This doc says the library won't control the cookies to keep the state anymore.

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 the requestAccessToken to popup the auth:

tokenClient = google.accounts.oauth2.initTokenClient({
          client_id: 'YOUR_CLIENT_ID',
          scope: 'https://www.googleapis.com/auth/calendar.readonly',
          prompt: 'consent',
          callback: tokenCallback
      });
tokenClient.requestAccessToken({prompt: ''})

In your tokenCallback you can save the credentials you get somehow, e.g.:

const tokenCallback(credentials) => {
    // save here the credentials using localStorage or cookies or whatever you want to.
}

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 to gapi, like:

gapi.load('client', function() {
    gapi.client.init({}).then(function() {
      let credentials = // get your credentials from where you saved it
      credentials = JSON.parse(credentials); // parse it if you got it as string
      gapi.client.setToken(credentials);

      ... continue you app ...
    }).catch(function(err) {
      // do catch...
    });
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文