Php 致命错误:调用数组上的成员函数getAttributes()
为了更好地理解如何使用google signin api,我一直在学习google认证教程,最近我收到了以下错误:Php 致命错误:调用数组上的成员函数getAttributes(),php,google-authentication,Php,Google Authentication,为了更好地理解如何使用google signin api,我一直在学习google认证教程,最近我收到了以下错误: Fatal error: Call to a member function getAttributes() on array. 每当我试图: $this->client->verifyIdToken()->getAttributes(); 在getPayload()函数中。我不知道为什么会这样。我的配置是Windows 10,我正在使用WAMP服务器运行此
Fatal error: Call to a member function getAttributes() on array.
每当我试图:
$this->client->verifyIdToken()->getAttributes();
在getPayload()函数中。我不知道为什么会这样。我的配置是Windows 10,我正在使用WAMP服务器运行此应用程序。任何帮助都将不胜感激
<?php class GoogleAuth {
private $db;
private $client;
public function __construct(Google_Client $googleClient)
{
$this->client = $googleClient;
$this->client->setClientId('234sfsdfasdfasdf3223jgfhjghsdsdfge3.apps.googleusercontent.com');
$this->client->setClientSecret('fD5g4-B6e5dCDGASefsd-');
$this->client->setRedirectUri('http://localhost:9080/GoogleSigninTutorial/index.php');
$this->client->setScopes('email');
}
public function checkToken()
{
if(isset($_SESSION['access_token']) && !empty($_SESSION['access_token']))
{
$this->client->setAccessToken($_SESSION['access_token']);
}
else
{
return $this->client->createAuthUrl();
}
return '';
}
public function login()
{
if(isset($_GET['code']))
{
$this->client->authenticate($_GET['code']);
$_SESSION['access_token'] = $this->client->getAccessToken();
return true;
}
return false;
}
public function logout()
{
unset($_SESSION['access_token']);
}
public function getPayload()
{
return $this->client->verifyIdToken()->getAttributes();
}
}
?>
我也有同样的问题。
据我所知
$attributes = $this->client->verifyIdToken()->getAttributes();
是一种过时的访问数组的方法,该数组应该返回google帐户的信息(即,在运行此行之后,$attributes应该是一个数组,其中包含与令牌对应的google帐户的所有信息。)
试试这个
$this->client->verifyIdToken();
在最新的api中(到目前为止),这一行本身返回一个包含预期信息的数组(这就是为什么在添加->getAttributes()
时会出现错误,因为在数组上调用此函数时无效。)
因此,只需在上面运行这一行来生成数组,如果您想查看值,请将其放入echo中,如下所示
echo '<pre>', print_r($attributes), '</pre>';
希望这能有所帮助。谢谢!!!节省了我的时间!只有一件事,当设置作用域时,指定您想要的。。。检查这里…阿尔比斯说得对!此外,如果您不确定所指的是什么,您可以在这里找到所有作用域,只需使用Ctrl+F(Cmd+F)在所需的作用域中查找特定的关键字即可。
$this->client->verifyIdToken()['email'];
$this->client->verifyIdToken()['name'];
//so on