Fatal error: Call to a member function getAttributes() on array
Fatal error: Call to a member function getAttributes() on array
我一直在做这个 google 身份验证教程,以更好地了解如何使用 google 登录 api,我最近收到此错误:
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();
是一种过时的访问数组的方法,该数组应该 return google 帐户的信息(即在 运行 宁此行之后, $attributes 应该是一个数组以及令牌对应的google账户的所有信息。)
试试这个
$this->client->verifyIdToken();
似乎在最新的 api(到目前为止)中,这一行本身 return 是一个包含预期信息的数组(这就是为什么在添加 ->getAttributes()
, 因为这个函数在数组上调用时无效。)
所以简单地 运行 上面的这一行来生成数组,如果你想看到值,把它放在 echo 中,就像这样
echo '<pre>', print_r($attributes), '</pre>';
如果您没有看到显示任何数组,可能是您有一个
header('Location: url')
在执行此 echo 后立即重定向到另一个 URL 地址的某个地方,因此它永远不会显示。 (或 die
)
您还可以通过执行
直接访问 email
、name
、given_name
、family_name
等特定属性
$this->client->verifyIdToken()['email'];
$this->client->verifyIdToken()['name'];
//so on
希望对您有所帮助。
我一直在做这个 google 身份验证教程,以更好地了解如何使用 google 登录 api,我最近收到此错误:
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();
是一种过时的访问数组的方法,该数组应该 return google 帐户的信息(即在 运行 宁此行之后, $attributes 应该是一个数组以及令牌对应的google账户的所有信息。)
试试这个
$this->client->verifyIdToken();
似乎在最新的 api(到目前为止)中,这一行本身 return 是一个包含预期信息的数组(这就是为什么在添加 ->getAttributes()
, 因为这个函数在数组上调用时无效。)
所以简单地 运行 上面的这一行来生成数组,如果你想看到值,把它放在 echo 中,就像这样
echo '<pre>', print_r($attributes), '</pre>';
如果您没有看到显示任何数组,可能是您有一个
header('Location: url')
在执行此 echo 后立即重定向到另一个 URL 地址的某个地方,因此它永远不会显示。 (或 die
)
您还可以通过执行
直接访问email
、name
、given_name
、family_name
等特定属性
$this->client->verifyIdToken()['email'];
$this->client->verifyIdToken()['name'];
//so on
希望对您有所帮助。