【问题标题】:Google+ vs Google Identity Platform APIGoogle+ 与 Google 身份平台 API
【发布时间】:2023-03-31 20:24:01
【问题描述】:

tl;dr: 有人能解释一下这两个平台在实现客户端 Google 登录流程方面到底有什么区别吗?

背景故事:

我一直在尝试实现客户端 Google 登录到我的网站。首先,我使用标签实现了具有全局设置的 Google+ 平台,因此可以监控用户会话。在此处获取信息:https://developers.google.com/+/web/signin/

但是,我遇到了一个问题,如果用户未登录,网站会自动检查用户登录状态,这导致了许多“退出”的“toastr”消息,我在 signInCallback 函数中实现了这些消息。这很烦人。

所以我做了一些研究,偶然发现了他们的“快速启动应用程序”并浏览了它。它比他们的指南复杂得多,许多元素都记录在 Google Identity Platform 上,这里: https://developers.google.com/identity/sign-in/web/reference

现在我真的不明白实现他们的登录的正确方法是什么——是带有标签回调检查用户状态的轻量级 Google+ 按钮,还是带有侦听器、gapi 实例等的强大 GIP 方式?这些平台有什么不同?

【问题讨论】:

    标签: javascript web client google-plus google-identity


    【解决方案1】:

    Google+ 平台登录 (gapi.auth) 和身份平台 (gapi.auth2) 都相关且工作相似。

    两者的主要区别是:

    gapi.auth2 支持更现代的 JavaScript (listeners and promises),因此您可以这样做:

    var signinChanged = function (val) {
      console.log('Signin state changed to ', val);
      document.getElementById('signed-in-cell').innerText = val;
    };
    
    auth2.isSignedIn.listen(signinChanged);
    

    ...auth2 具有更明确的语法,让您可以更好地控制行为:

    gapi.load('auth2', function() {
      auth2 = gapi.auth2.init({
        client_id: 'CLIENT_ID.apps.googleusercontent.com',
        fetch_basic_profile: true,
        scope: 'profile'
      });
    
      // Sign the user in, and then retrieve their ID.
      auth2.signIn().then(function() {
        console.log(auth2.currentUser.get().getId());
      });
    });
    

    auth2 提供基本配置文件支持,无需额外的 API 调用:

    if (auth2.isSignedIn.get()) {
      var profile = auth2.currentUser.get().getBasicProfile();
      console.log('ID: ' + profile.getId());
      console.log('Name: ' + profile.getName());
      console.log('Image URL: ' + profile.getImageUrl());
      console.log('Email: ' + profile.getEmail());
    }
    

    简而言之,我建议使用https://developers.google.com/identity/sign-in/ 中记录的方法,例如https://developers.google.com/identity/sign-in/web/

    正确实现登录将取决于您想要什么样的登录:

    • 仅限客户端,您可以只使用 JavaScript/iOS/Android 客户端
    • 混合客户端-服务器身份验证,您需要实现类似于快速入门之一的内容

    如果你只做客户端,那么它应该很简单:你授权用户,然后使用 API 客户端访问资源。如果您正在做一些更复杂的事情,例如管理会话等,在使用授权码授权您的服务器后,您应该使用 API 客户端的 ID 令牌来授权用户的会话。

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2017-08-02
      • 2020-11-07
      • 2019-12-23
      • 2020-11-09
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多