This post explains how to implement Login / Sign-In with Google
Introduction :- Google Sign-In helps to increase the customers on your website. Because current trend is almost all the users have a Google Account and they can login to their Google account without having to register their website. It make easy for the users to register.
Advantages of Google login :-
1. Faster registration.
2. Less to remember different accounts.
3. Less failed attempts.
Steps to implement google login :-
Step 1: Firstly, Create Client ID, Client secret key and also define redirect url of project on Google developer console (https://console.developers.google.com).
Step 2: In this step define the App Client ID and Client Secret Key (Settings.php)
<?php /* Google Client Id */ define('CLIENT_ID', 'xxxxxxxxxxxxxxxxxxxx'); /* Google Client Secret */ define('CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxx'); /* Google Redirect Url */ define('CLIENT_REDIRECT_URL', 'xxxxxxxxxxxxxxxxxxxx'); ?>
Step 3:- Now add the link in your code (index.php)
<?php require_once('settings.php'); $login_url = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online'; ?> <html> <head> <title> Login with Google </title> </head> <body> <a href="<?= $login_url ?>">Login with Google</a> </body> </html>
The “login url” is basically https://accounts.google.com/o/oauth2/v2/auth with five parameters :
1. scope :- It is used for what you want to do or what you want to get from the user.
For example:- In this “Login url” we have added 3 scopes.
2. redirect_uri : Your redirect url.
3. Response_type : It is set to the default value of “Code”.
4.Client_id : Your Google App Client Id.
5.Access_type : It is set to “online”.
When a user clicks on this link, the user will be redirected to Google where he or she is logged in. After logging in, Google redirects users to the redirect URL provided by you.
Step 3 :Redirect URL (googleauth.php)
When the user will be redirected on provided URL Google will pass a parameter as a GET parameter whose name will be “code”. You will need to use this parameter “code” and hit the API to get access token.
After you get the token you can make another API call to get the user profile information.
<?php require_once('settings.php'); //Google application Client Id, Client Secret and Redirect Url require_once('google-login-api.php'); // It’s Holds the various APIs involved as a PHP class if (isset($_GET['code'])) { try { $googleapi = new Google(); // Get token from google $TokenData = $googleapi>Get_Token(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']); // Select token from token Data array $access_token = $TokenData['access_token']; // Get user information from google $userprofile_information = $googleapi>Get_User_Information($access_token); echo '<pre>'; print_r($userprofile_information); echo '</pre>'; // if user logged successfully then redirected to home page header('Location: homepage.php'); } catch (Exception $e) { echo $e->getMessage(); exit(); } } ?>
This is the code to get the access token using the authorization code and get User Profile Information (google-login-api.php):
<?php class Google { public function Get_Token($client_id, $redirect_uri, $client_secret, $code) { $url = 'https://accounts.google.com/o/oauth2/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 : While fetch the token'); return $data; } public function Get_User_Information($access_token) { $url = 'https://www.googleapis.com/plus/v1/people/me'; $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 : While get the user information'); return $data; } } ?>