我正在一个项目上,我想实现Google和Facebook登录按钮。我要做的第一件事是去官方指南,测试这个简单的代码,它的工作。
<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);
};
</script>
</body>
</html>
现在,我想将接收到的数据与数据库连接起来,并且由于我使用javascript获取了数据,所以我将其与AJAX一起发送给具有3个参数(电子邮件,密码,方法)的PHP通用函数。在方法中,我希望这三个值之一(“ GOOGLE”,“ FACEBOOK”,“ FORM”)知道它是哪种连接。使用“ GOOGLE”,我发送id_token而不是密码。
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
var posting = $.post(
"ajax.php",
{
"AjaxPostQuery": "Connect",
"ConnLogin": profile.getEmail(),
"ConnPWD": googleUser.getAuthResponse().id_token,
"method" : "GOOGLE"
}
);
posting.done(function(data) {
console.log(data);
$("#connectBox").empty().append(data);
if(data === ""){
$("#connectBox").empty().append("You are connected");
location.reload();
}
});
}
问题