admin管理员组文章数量:1530517
谷歌gmail也是很常用了,第三方登录也和qq微信那样一样方便。
如下就详解怎么对接,谷歌三方登录申请非常简单创建即用。
测试体验地址:http://tool.apizl/User/Init/login.html
首先需要先新增一个凭据:
创建凭据 -> OAuth 客户端 ID -> 网页应用,之后输入 JavaScript 来源、重定向 URI
https://console.developers.google/apis/credentials
新增一个oauth,然后一步步往下就行。
添加一个主域和回调域名,
主域名和回调域名可以为本地地址!
在需要调用谷歌登录的地方加入如下HTML代码 ,官方调用示例:
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent">
<script src="https://apis.google/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
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);
};
//注销
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
alert('用户注销成功');
});
}
</script>
</body>
</html>
前台获取相关谷歌用户登录信息 https://developers.google/identity/sign-in/web/
然后拿到id_token去后台进行校验:
require_once 'vendor/autoload.php';
// Get $id_token via HTTPS POST.
$client = new Google_Client(['client_id' => $CLIENT_ID]); // Specify the CLIENT_ID of the app that accesses the backend
$payload = $client->verifyIdToken($id_token);
if ($payload) {
$userid = $payload['sub'];
// If request specified a G Suite domain
} else {
// Invalid ID token
}
sub和前台获取到的id进行对比,如果一致就是登陆成功。
当然还有另外一种方式直接get校验!
https://www.googleapis/oauth2/v3/tokeninfo?id_token=xxxxx
会获取到如下json数据,进行判断校验即可。
{
// These six fields are included in all Google ID Tokens.
"iss": "https://accounts.google",
"sub": "110169484474386276334",
"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent",
"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent",
"iat": "1433978353",
"exp": "1433981953",
// These seven fields are only included when the user has granted the "profile" and
// "email" OAuth scopes to the application.
"email": "testuser@gmail",
"email_verified": "true",
"name" : "Test User",
"picture": "https://lh4.googleusercontent/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
"given_name": "Test",
"family_name": "User",
"locale": "en"
}
后台校验文档:https://developers.google/identity/sign-in/android/backend-auth?hl=zh-cn
文章地址:https://www.apizl/archives/view-148749-1.html
版权声明:本文标题:让自己网站对接google谷歌第三方登录接口详解说明 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1725992120a1051620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论