我正在学习Google的API 0auth身份验证登录系统的基础知识。
我正在使用CodexWorld上的教程。虽然这个有用,但它使用的是丑陋的图片,而不是“真正的”谷歌登录按钮(来自 g-signin2 类),这是我个人无法得到的过度。
我尝试用该按钮替换图片并添加了所需的元素以便使用它。但现在它打开了2个窗口,而不仅仅是1个通过php
如何防止由g-signin2
- 按钮打开的第二个窗口打开?
感谢您的帮助!
VicStudio
这是我修改过的代码。请注意我是如何用真实的google-signin2
按钮替换图片按钮并添加所需的元素
<?php
//Include GP config file && User class
include_once 'gpConfig.php';
include_once 'classuser.php';
if(isset($_GET['code'])){
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
//Get user profile data from google
$gpUserProfile = $google_oauthV2->userinfo->get();
//Initialize User class
$user = new User();
//Insert or update user data to the database
$gpUserData = array(
'oauth_provider'=> 'google',
'oauth_uid' => $gpUserProfile['id'],
'email' => $gpUserProfile['email'],
'link' => $gpUserProfile['link']
);
$userData = $user->checkUser($gpUserData);
//Storing user data into session
$_SESSION['userData'] = $userData;
//Render facebook profile data
if(!empty($userData)){
$output = '<h1>Google+ Profile Details </h1>';
$output .= '<br/>Google ID : ' . $userData['oauth_uid'];
$output .= '<br/>Email : ' . $userData['email'];
$output .= '<br/>Logged in with : Google';
$output .= '<br/><a href="'.$userData['link'].'" target="_blank">Click to Visit Google+ Page</a>';
$output .= '<br/>Logout from <a href="logout.php">Google</a>';
}else{
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
}
} else {
//New, modified code:
$authUrl = $gClient->createAuthUrl();
$authUrl = filter_var($authUrl, FILTER_SANITIZE_URL);
$output = "<div class='g-signin2' data-onsuccess='onSignIn' id='glog'></div> <script>document.getElementById('glog').onclick = function () { window.location.href = '".$authUrl."'; } </script>";
//Original Code:
// $authUrl = $gClient->createAuthUrl();
// $output = '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="images/glogin.png" alt=""/></a>';
}
?>
<html>
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="246194741577-gajd75di0cam2j9u4q69l9trvg88hqkc.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div><?php echo $output; ?></div>
</body>
</html>