Create a Google App from Google API Console
create a project by clicking "Select a project" (at the top), and then clicking on the "Add project" button in the popup. In the next screen enter your project name, and agree with T&C.
Now go to the Library tab on the left. and search "Google+ API" and enable it.
Now go to the Credentials tab on the left. In the next screen click on "OAuth consent screen". Fill out the mandatory fields and save it.
Now click on the "Credentials" tab. In the same screen, click on "Create credentials". Choose "OAuth Client ID" as the type.
In the next screen fill out the name. The Application type should be "Web application"Add redirect url in the section Authorised redirect URIs. This url should point to your redirect url script. Also you can add a localhost url. and save it.
After save, you will get the App Client ID and App Secret.
Create a "config.php" file
/* App Client Id */
define('APP_CLIENT_ID', 'YOUR_APP_CLIENT_ID');
/* App Client Secret */
define('APP_CLIENT_SECRET', 'YOUR_APP_CLIENT_SECRET');
/* App Redirect Url */
define('APP_CLIENT_REDIRECT_URL', 'YOUR_APP_CLIENT_REDIRECT_URL');
Create a "index.php" file
Copy following code and past in index file into the head tag with using the style tag
.wrapper{
width:800px;
text-align:center;
margin: 50px auto;
}
.btngoogle{
text-decoration: none;
color: #fff;
background: #D84830;
font-weight: bold;
padding: 8px 40px;
font-size: 26px;
}
Now add the following button code in body tag
<div class="wrapper">
<a class="btngoogle" href="<?= '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(APP_CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . APP_CLIENT_ID . '&access_type=online' ?>">Login with Google</a>
</div>
Now create a "result.php" file for get access token and user profile using API
Add following code
include('config.php');
// Getting access token
public function GetAccessToken($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 : Failed to receieve access token');
return $data;
}
// Getting user profile information
public function GetUserProfileInfo($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 : Failed to get user information');
return $data;
}
// Google will retun the code parameter after verification
if(isset($_GET['code'])) {
try {
// Get the access token
$data = GetAccessToken(APP_CLIENT_ID, APP_CLIENT_REDIRECT_URL, APP_CLIENT_SECRET, $_GET['code']);
// Get user information
$userinfo = GetUserProfileInfo($data['access_token']);
// Here yoiu can see all user detail
echo '';print_r($userinfo); echo '
';
// Now that the user is logged in you may want to start some session variables
$_SESSION['logged_in'] = 1;
}
catch(Exception $e) {
echo $e->getMessage();
exit();
}
}