Google+与Google Identity Platform API

Ide*_*ixx 9 javascript client google-plus web google-identity

tl; dr:有人可以解释在这两个平台之间实现客户端Google登录流程的不同之处究竟是什么?

背景故事:

我一直在努力实现客户端Google登录我的网站.首先,我使用标签实施了全球设置的Google+平台,因此可以监控用户会话.得到的信息:https://developers.google.com/+/web/signin/

但是,我遇到了一个问题,如果用户没有登录,网站将自动检查用户登录状态,这导致许多'toastr'消息'Logged out',我在signInCallback函数中实现.这真令人生气.

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

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

cla*_*ass 13

Google+平台登录(gapi.auth)和Identity平台(gapi.auth2)都是相关的,并且工作方式类似.

两者之间的主要区别是:

gapi.auth2支持更现代的JavaScript(听众和承诺),所以你可以这样做:

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

auth2.isSignedIn.listen(signinChanged);
Run Code Online (Sandbox Code Playgroud)

... 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());
  });
});
Run Code Online (Sandbox Code Playgroud)

并且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());
}
Run Code Online (Sandbox Code Playgroud)

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

正确实施登录取决于您想要的登录类型:

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

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