count() method needs an array as parameter regarding "$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);"

Viewed 35

I am trying to integrate google login on my website. I have added compose and installed google auth version 2.0.

code for config.php

<?php

//start session on web page
session_start();

//config.php

//Include Google Client Library for PHP autoload file
require_once 'vendor/autoload.php';

//Make object of Google API Client for call Google API
$google_client = new Google_Client();

//Set the OAuth 2.0 Client ID
$google_client->setClientId('XXXXXX');

//Set the OAuth 2.0 Client Secret key
$google_client->setClientSecret('XXXXXX');

//Set the OAuth 2.0 Redirect URI
$google_client->setRedirectUri('http://localhost/MMCOE_LINER/basic_login/login.php');

// to get the email and profile 
$google_client->addScope('email');

$google_client->addScope('profile');

?>

code for index.php

<?php


//Include Configuration File
include('config.php');

$login_button = '';


if(isset($_GET["code"]))
{

    $token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);


 if(!isset($token['error']))
 {
 
  $google_client->setAccessToken($token['access_token']);

 
  $_SESSION['access_token'] = $token['access_token'];


  $google_service = new Google_Service_Oauth2($google_client);

 
  $data = $google_service->userinfo->get();
  echo $data;

 
  if(!empty($data['given_name']))
  {
   $_SESSION['user_first_name'] = $data['given_name'];
  }

  if(!empty($data['family_name']))
  {
   $_SESSION['user_last_name'] = $data['family_name'];
  }

  if(!empty($data['email']))
  {
   $_SESSION['user_email_address'] = $data['email'];
  }

  if(!empty($data['gender']))
  {
   $_SESSION['user_gender'] = $data['gender'];
  }

  if(!empty($data['picture']))
  {
   $_SESSION['user_image'] = $data['picture'];
  }
 }
}


if(!isset($_SESSION['access_token']))
{

 $login_button = '<a href="'.$google_client->createAuthUrl().'">Login With Google</a>';
}

?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>PHP Login using Google Account</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
 
 </head>
 <body>
  <div class="container">
   <br />
   <h2 align="center">PHP Login using Google Account</h2>
   <br />
   <div class="panel panel-default">
   <?php
   if($login_button == '')
   {
    echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
    echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />';
    echo '<h3><b>Name :</b> '.$_SESSION['user_first_name'].' '.$_SESSION['user_last_name'].'</h3>';
    echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
    echo '<h3><a href="logout.php">Logout</h3></div>';
   }
   else
   {
    echo '<div align="center">'.$login_button . '</div>';
   }
   ?>
   </div>
  </div>
 </body>
</html>

I am running it on Xampp

So, after running index.php and selecting my Gmail account I am getting an error, the complete error:

Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:67 Stack trace: 
#0 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(67): count(NULL) 
#1 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(107): GuzzleHttp\Handler\CurlFactory->release(Object(GuzzleHttp\Handler\EasyHandle)) 
#2 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) 
#3 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array) 
#4 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(51): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array) 
#5 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php(72): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array) 
#6 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Middleware.php(30): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array) 
#7 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php(68): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array) 
#8 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Middleware.php(57): GuzzleHttp\RedirectMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array) 
#9 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\HandlerStack.php(67): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array) 
#10 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Client.php(268): GuzzleHttp\HandlerStack->__invoke(Object(GuzzleHttp\Psr7\Request), Array) 
#11 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Client.php(96): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array) 
#12 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Client.php(104): GuzzleHttp\Client->sendAsync(Object(GuzzleHttp\Psr7\Request), Array) 
#13 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\google\auth\src\HttpHandler\Guzzle6HttpHandler.php(34): GuzzleHttp\Client->send(Object(GuzzleHttp\Psr7\Request), Array) 
#14 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\google\auth\src\OAuth2.php(492): Google\Auth\HttpHandler\Guzzle6HttpHandler->__invoke(Object(GuzzleHttp\Psr7\Request)) 
#15 C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\google\apiclient\src\Google\Client.php(184): Google\Auth\OAuth2->fetchAuthToken(Object(Google\Auth\HttpHandler\Guzzle6HttpHandler)) 
#16 C:\xampp\htdocs\mmcoe_liner\basic_login\login.php(15): Google_Client->fetchAccessTokenWithAuthCode('4/0ARtbsJrk6JH4...') 
#17 {main} thrown in C:\xampp\htdocs\mmcoe_liner\basic_login\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 67

Also, after some debugging I found out the error is here :

$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
1 Answers
$data = $google_service->userinfo->get();

You appear to be under the assumption that the userinfo endpoint returns information like gender and name. Which it does not. You cant even guarantee that it will return email and name sometimes.

Try doing a print on the data var to see what is actually being returned

print_r($data);

That is why we use the people.get method as you have already requested profile scope you should have access to it.

Related