我一直在做谷歌認證教程,以更好地瞭解如何使用谷歌登入API和我最近收到此錯誤:致命錯誤:調用一個成員函數的getAttributes()的陣列
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();
}
}
?>
謝謝!爲我節省了時間!只是一件事,當設置範圍指定你想要什麼...檢查這裏... http://stackoverflow.com/questions/14007560/get-userinfo-from-google-oauth-2-0-php-api – Albeis
好點Albeis!另外,您可以在這裏找到所有範圍https://developers.google.com/identity/protocols/googlescopes只需使用Ctrl + F(Cmd + F)在您需要的範圍內查找特定關鍵字確定它被稱爲。 – user