John Davidson

Why does isset($_GET['code']) returning false while using google oauth login in php?

0 comments
Message:


 include ("header.php") ;
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->setRedirectUri('http://localhost/ecommerce/index.php');
$client->addScope('email');
$client->addScope('profile');
$login_button=' <div class="col-sm-4">
<div class="card">
<div class="card-body">
<a class="btn btn-social btn-google" href="'.$client->createAuthUrl().'" role="button">
<i class="fab fa-google"></i>
Sign in with Google
</a>
</div>
</div>
</div>';

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

$token=$client->fetchAccessTokenWithAuthCode($_GET['code']);
if(!isset($token['error'])){
$client->setAccessToken($token['access_token']);
$_SESSION['access_token']=$token['access_token'];
$google_service=new Google_Service_Oauth2($client);
$data=$google_service->userinfo->get();
if(!empty($data['given_name'])){
$_SESSION['uid']=$data['given_name'];
}
if(!empty($data['family_name'])){
$_SESSION['user_last_name']=$data['family_name'];
}
if(!empty($data['gender'])){
$_SESSION['gender']=$data['gender'];
}
}
}


?>

My code does not enter the if block: isset($_GET['code']) is returning false.
I want access to just the user name and gender.
I have installed composer and google api client 2.0.



My Answer:
The isset() function is used in PHP to determine if a variable is set and is not NULL. In the case of $_GET[], isset() is used to check if a specific key exists in the $_GET superglobal array. This is commonly used to check if a query parameter has been passed in the URL.

Rate this post

5 of 5 based on 9292 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us