让我的用户使用 Google 登录的首选 JavaScript 方法实际上是什么?
What is actually the preferred JavaScript method to let my users sign in with Google?
我一直在努力将我的网站(使用服务器端、基于 Go 的 OpenID 解决方案,上周一 Google 可能已禁用也可能未禁用)转换为 Google JavaScript oauth 库使我的用户能够使用他们的 Google 帐户登录。我首先通过提问 , and received several comments from people trying to help, but could get no definite answers. I then decided to just play it safe and convert to another method, which seemed to go well at first, but I now have some complaints from my users that they cannot get signed in, see my next question .
联系了我
我现在的问题是,我遇到了至少四套针对不同 api 库的不同文档,全部在官方 Google 网站上,它们都声称告诉我如何执行此操作.排名不分先后:
- Google APIs Client Library for JavaScript (Beta)
- Google+ Platform, with a Quick Start
- Google Identity Platform
- Google Sign-In for Websites
我现在对实际的 "preferred" 方法可能是什么感到非常困惑,并且想知道我是否可能使用了可能导致我的问题的过时方法?我目前正在使用上面列表中选项 #2 中快速入门指南中使用的方法。
如有任何见解,我们将不胜感激。
简短版:使用 Google 登录网站。要从 OpenID2 迁移:
https://developers.google.com/identity/sign-in/auth-migration#oid2
如果您must/strongly更喜欢直接使用标准 OAuth2:
https://developers.google.com/identity/protocols/OpenID2Migration
客户端与 Google 身份平台 Javascript API(Google 网站登录)的基本集成如下所示:
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOURCLIENTID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-openidrealm="YOUR_REALM" data-onsuccess="onSignIn"></div>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // NB. don't send this directly to your server, as that is insecure. Instead, send the full id_token, which your server can extract the id from using the 'sub' value.
console.log("Name: " + profile.getName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
};
</script>
</body>
</html>
以上登录用户,并为您提供他们的 ID 令牌。您需要将 "YOURCLIENTID.apps.googleusercontent.com" 替换为您在 Developers Console 中注册的客户端 ID(创建项目,然后导航至 APIs & auth -> Credentials -> Create a new Client ID)。请务必在 "Authorized JavaScript origins" 列表中指定您的生产和开发域。还将该示例中的 "YOUR_REALM" 替换为您之前的 OpenID 2.0 领域。
拥有 "id_token" 后,您可以使用后端进行身份验证。为此,您可以将 "id_token" 传递到后端,然后使用 JWT 库对其进行验证和解码。特别是对于 OpenID 迁移,您需要将 "openid_id" 值从 JWT 映射到新的 "sub" ID。
id_token 验证的一些示例代码在这里:
为了测试,您可以使用 this tool 解码 ID 令牌以查看它包含的数据(它应该包含 openid_id
作为声明)。
关于文档:对于登录,首选方法是 Google 网站登录(#4)。它实现了最简单和最新的 API 登录。
在后台,Google 登录是 OAuth2/OpenIDConnect 的一个实现。 Link 上面的#3 描述了使用这个标准流程,以及整页重定向。这是一个受支持的流程,但如 link 所述,Google 网站登录是首选。
Google APIs 客户端库 (#1) 在后台使用 OAuth。它的示例代码描述了一个旧的、遗留的身份验证模型,应该更新。我们很快就会这样做;感谢关注
最后,随着最近推出的 Google 网站登录,Google+ 平台 API (#2) 不再是登录的首选方法。我们也正在为此更新文档,以避免将来出现混淆。
我一直在努力将我的网站(使用服务器端、基于 Go 的 OpenID 解决方案,上周一 Google 可能已禁用也可能未禁用)转换为 Google JavaScript oauth 库使我的用户能够使用他们的 Google 帐户登录。我首先通过提问
我现在的问题是,我遇到了至少四套针对不同 api 库的不同文档,全部在官方 Google 网站上,它们都声称告诉我如何执行此操作.排名不分先后:
- Google APIs Client Library for JavaScript (Beta)
- Google+ Platform, with a Quick Start
- Google Identity Platform
- Google Sign-In for Websites
我现在对实际的 "preferred" 方法可能是什么感到非常困惑,并且想知道我是否可能使用了可能导致我的问题的过时方法?我目前正在使用上面列表中选项 #2 中快速入门指南中使用的方法。
如有任何见解,我们将不胜感激。
简短版:使用 Google 登录网站。要从 OpenID2 迁移: https://developers.google.com/identity/sign-in/auth-migration#oid2
如果您must/strongly更喜欢直接使用标准 OAuth2: https://developers.google.com/identity/protocols/OpenID2Migration
客户端与 Google 身份平台 Javascript API(Google 网站登录)的基本集成如下所示:
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOURCLIENTID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-openidrealm="YOUR_REALM" data-onsuccess="onSignIn"></div>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // NB. don't send this directly to your server, as that is insecure. Instead, send the full id_token, which your server can extract the id from using the 'sub' value.
console.log("Name: " + profile.getName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
};
</script>
</body>
</html>
以上登录用户,并为您提供他们的 ID 令牌。您需要将 "YOURCLIENTID.apps.googleusercontent.com" 替换为您在 Developers Console 中注册的客户端 ID(创建项目,然后导航至 APIs & auth -> Credentials -> Create a new Client ID)。请务必在 "Authorized JavaScript origins" 列表中指定您的生产和开发域。还将该示例中的 "YOUR_REALM" 替换为您之前的 OpenID 2.0 领域。
拥有 "id_token" 后,您可以使用后端进行身份验证。为此,您可以将 "id_token" 传递到后端,然后使用 JWT 库对其进行验证和解码。特别是对于 OpenID 迁移,您需要将 "openid_id" 值从 JWT 映射到新的 "sub" ID。
id_token 验证的一些示例代码在这里:
为了测试,您可以使用 this tool 解码 ID 令牌以查看它包含的数据(它应该包含 openid_id
作为声明)。
关于文档:对于登录,首选方法是 Google 网站登录(#4)。它实现了最简单和最新的 API 登录。
在后台,Google 登录是 OAuth2/OpenIDConnect 的一个实现。 Link 上面的#3 描述了使用这个标准流程,以及整页重定向。这是一个受支持的流程,但如 link 所述,Google 网站登录是首选。
Google APIs 客户端库 (#1) 在后台使用 OAuth。它的示例代码描述了一个旧的、遗留的身份验证模型,应该更新。我们很快就会这样做;感谢关注
最后,随着最近推出的 Google 网站登录,Google+ 平台 API (#2) 不再是登录的首选方法。我们也正在为此更新文档,以避免将来出现混淆。