This is a simple system where for users to like a product they have to sign in to google in order to identify them. The button clicked contains value of product_id
but I cant seem to figure out how to retrieve it after sign in and getting redirected to another page after sign in. Notice that the form action redirects user to google sign in.
<form method="POST" action="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' .
urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') .
'&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">
<button name="upvote" type="submit" class="btn btn-info" value="<?php echo $product_id; ?>">Like</button>
</form>
The destination (Redirecting URL) after user has signed in is receiver.php
Here is my full working code;
1.settings.php THIS IS TO SIMPLY CONFIGURE THE API
<?php
/* Google App Client Id */
define('CLIENT_ID', '/*My client ID*/');
/* Google App Client Secret */
define('CLIENT_SECRET', '/*My client Secret*/');
/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'https://www.example.com/receier.php');
?>
2. google-login-api.php TO SEND MY KEYS TO GOOGLE AND TO GET USER DATA AFTER SIGNING IN
<?php
class GoogleLoginApi
{
public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {
$url = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to receieve access token');
return $data;
}
public function GetUserProfileInfo($access_token) {
$url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to get user information');
return $data;
}
}
?>
3. likeproduct.php THIS IS WHERE THE BUTTON THAT REDIRECTS TO GOOGLE SIGN IN IS CLICKED, THE BUTTON CONTAINS THE VALUE OF THE PRODUCT ID
<form method="POST" action="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' .
urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') .
'&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">
<button name="upvote" type="submit" class="btn btn-info" value="<?php echo $product_id; ?>">Like</button>
</form>
4.receiver.php THIS IS WHERE USER GOOGLE ACCOUNT DETAILS ARE OBTAINED (REDIRECTING URL) AND ALSO WHERE I INTENT TO GET THE $product_id
THAT WAS PASSED AS VALUE OF THE LIKE BUTTON IN likeproduct.php
<?php
require_once('settings.php');
require_once('google-login-api.php');
// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
try {
$gapi = new GoogleLoginApi();
// Get the access token
$data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
// Get user information
$user_info = $gapi->GetUserProfileInfo($data['access_token']);
}
catch(Exception $e) {
echo $e->getMessage();
exit();
}
}
?>
How can I get the product id on receiver.php after submit?