我正在使用嵌入我网页的“使用GOOGLE API登录”。点击网页上的链接,用户被重定向到谷歌,要求用户允许应用程序访问基本信息。但是,当用户重定向到主网页时,它不会显示用户信息。
以下是用户登陆
的主要 index.php<?php
require_once "app/init.php";
$gc = new Google_Client();
$auth = new GoogleAuth($gc);
if($auth->checkRedirectCode()){
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>MyTalaash</title>
</head>
<body>
<?php if(!$auth->isLoggedIn()):?>
<a href="<?php echo $auth->getAuthUrl();?>"> Sign in with Google</a>
<?php else:?>
<a href="signout.php">Sign Out</a>
<?php endif;?>
</body>
</html>
GoogleAuth.php中的代码如下
<?php
class GoogleAuth{
protected $client;
public function __construct(Google_Client $gc = null){
$this->client = $gc;
if($this->client){
$this->client->setClientId('SOME secret');
$this->client->setClientSecret('SOME secret');
$this->client->setRedirectUri('http://localhost/myTalaash/index.php');
$this->client->setScopes('https://www.googleapis.com/auth/plus.login');
}
}
public function isLoggedIn(){
return isset($_SESSION['access_token']);
}
public function getAuthUrl(){
return $this->client->createAuthUrl();
}
public function checkRedirectCode(){
if(isset($_GET['code'])){
$this->client->authenticate($_GET['code']);
$this->setToken($this->client->getAccessToken());
//$payload is supposed to contain user information but it doesnt
$payload = $this->getPayLoad();
echo "<pre>".print_r($payload,true)."</pre>";
return true;
}
return false;
}
public function setToken($token){
$_SESSION['access_token'] = $token;
$this->client->setAccessToken($token);
}
public function logout(){
unset($_SESSION['access_token']);
}
// The below function is responsible to get user information
public function getPayLoad(){
$payLoad = $this->client->verifyIdToken()->getAttributes();
return $payLoad;
}
}
答案 0 :(得分:0)
将此行更改为 index.php 文件..
if($auth->checkRedirectCode()){
header("Location: index.php");
}
用这个..
if($auth->checkRedirectCode()){
echo '<pre>', print_r( $auth->user ), '</pre>';
}
然后更改您的GoogleAuth.php文件。
class GoogleAuth { protected $client;public $user;
public function __construct(Google_Client $gc = null){
$this->client = $gc;
if($this->client){
$this->client->setClientId('SOME secret');
$this->client->setClientSecret('SOME secret');
$this->client->setRedirectUri('http://localhost/myTalaash/index.php');
$this->client->setScopes('https://www.googleapis.com/auth/plus.login');
}
}
public function isLoggedIn(){
return isset($_SESSION['access_token']);
}
public function getAuthUrl(){
return $this->client->createAuthUrl();
}
public function checkRedirectCode(){
if(isset($_GET['code'])){
$this->client->authenticate($_GET['code']);
$this->setToken($this->client->getAccessToken());
//$payload is supposed to contain user information but it doesnt
$this->user = $this->getPayLoad();
return true;
}
return false;
}
public function setToken($token){
$_SESSION['access_token'] = $token;
$this->client->setAccessToken($token);
}
public function logout(){
unset($_SESSION['access_token']);
}
// The below function is responsible to get user information
public function getPayLoad(){
$payLoad = $this->client->verifyIdToken();
return $payLoad;
}}