代码之家  ›  专栏  ›  技术社区  ›  ajayv

在谷歌令牌模型中,如何处理访问令牌到期和谷歌登录状态?

  •  0
  • ajayv  · 技术社区  · 2 年前

    对于 google sign in ,因为谷歌不赞成这个平台。js库所以我们将使用google identity service库,并使用 Token Model .

    const client = google.accounts.oauth2.initTokenClient({
      client_id: 'YOUR_GOOGLE_CLIENT_ID',
      scope: 'https://www.googleapis.com/auth/calendar.readonly',
      callback: (response) => {
        ...
      },
    });
    

    为了触发用户体验流 client.requestAccessToken(); 每次单击 Google signin 按钮

    我不明白的一件事是如何处理访问令牌过期-

    根据文件-

    By design, access tokens have a short lifetime. If the access token expires prior to the end of the user's session, obtain a new token by calling requestAccessToken() from a user-driven event such as a button press.

    问题1 上述情况是否意味着我们需要从回调函数显式调用此函数?或者我们怎么知道访问令牌已过期?

    还有平台。js,我们有一个选项来检查用户是否已经登录谷歌-

    GoogleAuth = gapi.auth2.getAuthInstance();
    currentUser = GoogleAuth.currentUser.get();
    
    if (currentUser && currentUser.isSignedIn()) {
       // user is already loggedin in google
    } else {
       googleAuthPromise = GoogleAuth.signIn();
       googleAuthPromise.then(function (user) {
         // now user is loggedin in google
       });
    }
    

    问题2 但是在新的库中,这些方法是不可用的,如果用户已经登录到谷歌(通过不涉及任何自定义会话或cookie变量),我们如何实现这一点?

    0 回复  |  直到 2 年前