让自己网站对接google谷歌第三方登录接口详解说明

来源:csdn博客 分类: 文章浏览史 发布时间:2020-08-26 18:58:25 最后更新:2020-08-26 浏览:291
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-08-26 18:58:25

谷歌gmail也是很常用了,第三方登录也和qq微信那样一样方便。

如下就详解怎么对接,谷歌三方登录申请非常简单创建即用。

测试体验地址:http://tool.apizl.com/User/Init/login.html

首先需要先新增一个凭据:

创建凭据 -> OAuth 客户端 ID -> 网页应用,之后输入 JavaScript 来源、重定向 URI

https://console.developers.google.com/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.com">

<script src="https://apis.google.com/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.com/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.com/oauth2/v3/tokeninfo?id_token=xxxxx

会获取到如下json数据,进行判断校验即可。

{

// These six fields are included in all Google ID Tokens.

"iss": "https://accounts.google.com",

"sub": "110169484474386276334",

"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",

"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",

"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.com",

"email_verified": "true",

"name" : "Test User",

"picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",

"given_name": "Test",

"family_name": "User",

"locale": "en"

}

 

后台校验文档:https://developers.google.com/identity/sign-in/android/backend-auth?hl=zh-cn

文章地址:https://www.apizl.com/archives/view-148749-1.html

php技术微信