PHP Login with Google Account

untuk membuat website yang mampu melakukan login dengan account google

pertama kali website atau domain tersebut harus memiliki developer key yang di miliki google

1. login atau buat sebuah account di google mail (

2. buka url

3. lakukan langkah2 ini untuk membuat api key

pertama pada menu consent screen


lalu masuk pada menu credentials -> buat sebuah client idaa-01.create-client-id

dan sebuah key -> browser keyaa-02.create-browser-key

tampilan key secara completeaa-03.complete-api-key

4. download php library untuk OAth2

5. extract ke folder / path yg sesuai dengan javascript origins

6.  ubah 4 value ini  di file GoogleClientApi/config.php

// OAuth2 Settings, you can get these keys at
'oauth2_client_id' => 'Client ID',
'oauth2_client_secret' => 'Client secret',
'oauth2_redirect_uri' => 'Redirect URIs',

// The developer key, you get this at
'developer_key' => 'API key',

7. buat 2 file baru  google-view-profile.php dan google-login.php


edit file google-login.php


//include google api files
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

//start session

$gClient = new Google_Client();

$gClient->setApplicationName('Login to localhost');

$google_oauthV2 = new Google_Oauth2Service($gClient);

$authUrl = $gClient->createAuthUrl();

//HTML page start
echo '<html xmlns="">';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo '<title>Login with Google</title>';
echo '</head>';
echo '<body>';
echo '
<h1>Login with Google</h1>

echo '

.googleplus:before {
border-right: 0.075em solid rgba(0, 0, 0, 0.1);
box-shadow: 0.075em 0 0 rgba(255, 255, 255, 0.25);
content: "GW+";
font-family:"Helvetica Neue",sans-serif"
float: left;
font: 120%/1.65 zocial;
margin: 0 0.5em 0 0;
padding: 0 0.5em;
text-align: center;
text-decoration: none;
text-transform: none;
.googleplus {
background-color: #DD4B39;
font-size: 13px;
margin: 8px 4px;
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
-moz-user-select: none;
border-color: rgba(0, 0, 0, 0.2) rgba(0, 0, 0, 0.2) rgba(0, 0, 0, 0.4);
border-image: none;
border-radius: 0.3em;
border-style: solid;
border-width: 1px;
box-shadow: 0 0.08em 0 rgba(255, 255, 255, 0.4) inset, 0 0 0.1em rgba(255, 255, 255, 0.9) inset;
color: #FFFFFF;
cursor: pointer;
display: inline-block;
padding: 0 0.95em 0 0;
position: relative;
text-align: center;
text-decoration: none;
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
white-space: nowrap;


echo '<a href="'.$authUrl.'" >Login With Google+</a>';
echo '</body></html>';

edit file google-view-profile.php


//include google api files
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

//start session

$gClient = new Google_Client();
$gClient->setApplicationName('view user profile');

$google_oauthV2 = new Google_Oauth2Service($gClient);

// get the code
if (isset($_GET['code'])) {
$_SESSION['token'] = $gClient->getAccessToken();

if (isset($_SESSION['token']))
//Get user details if user is logged in
$user                 = $google_oauthV2->userinfo->get();
$user_id              = $user['id'];
$user_name            = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
$email                = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
$profile_url          = filter_var($user['link'], FILTER_VALIDATE_URL);
$profile_image_url    = filter_var($user['picture'], FILTER_VALIDATE_URL);
$personMarkup         = "$email
<div><img src='$profile_image_url?sz=50'></div>
$_SESSION['token']    = $gClient->getAccessToken();

echo '
<a href="'.$profile_url.'" target="_blank"><img src="'.$profile_image_url.'?sz=50" /></a>';
echo '
<a href="?reset=1">Logout</a>';

//list all user details
echo '
echo '</pre>

lakukan pengetesan

buka url

halaman permintaan

halaman data-data user yang login



Published by


just to remember what i had known :)

One thought on “PHP Login with Google Account”

Comments are closed.