3

I have been doing this google authentication tutorial to better understand how to use the google signin api and I have recently received this error:

Fatal error: Call to a member function getAttributes() on array. 

It occurs whenever I am trying to:

$this->client->verifyIdToken()->getAttributes();

in the getPayload() function. I do not know why this is happening. My configuration is Windows 10 and I am using WAMP server to run this application. Any help would be appreciated.

<?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();
}
}
?>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

1 Answers1

5

I've had the same problem. From what I seemed to understand,

$attributes = $this->client->verifyIdToken()->getAttributes();

is an outdated way to access the array supposed to return the information of the google account (i.e. after running this line, $attributes was expected to be an array with all the information of the google account corresponding to the token.)

Try this instead

$this->client->verifyIdToken();

It seems that in the latest api (so far), this line in itself returns an array with the expected information (that's why you get an error when you add ->getAttributes(), because this function is not valid when called on an array.) So simply run this line above to generate the array, and put it in echo if you want to see the values, like so

echo '<pre>', print_r($attributes), '</pre>';

If you don't see any array displayed, it may be that you have a header('Location: url') somewhere that is redirecting to another URL address right after executing this echo, thus it never shows. (Or a die)

You can also directly access specific attributes such as email, name, given_name, family_name by doing

$this->client->verifyIdToken()['email'];
$this->client->verifyIdToken()['name'];
//so on

Hope this can help.

Xerkus
  • 2,695
  • 1
  • 19
  • 32
ribbit
  • 1,183
  • 11
  • 14
  • 1
    Thanks!!! Saved me time! Just one thing, when setting the scopes specify what you want... check here...http://stackoverflow.com/questions/14007560/get-userinfo-from-google-oauth-2-0-php-api – Albeis Mar 16 '17 at 19:26
  • Good point Albeis! Also, you can find all the scopes here https://developers.google.com/identity/protocols/googlescopes Just use Ctrl+F (Cmd+F) to look for a specific keyword in the scope you need if you're not sure what it's referred to as. – ribbit Mar 17 '17 at 22:28