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 (http://gmail.com)

2. buka url https://cloud.google.com/console

3. lakukan langkah2 ini untuk membuat api key

pertama pada menu consent screen

aa-00.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 https://code.google.com/p/google-api-php-client/

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 https://code.google.com/apis/console
'oauth2_client_id' => 'Client ID',
'oauth2_client_secret' => 'Client secret',
'oauth2_redirect_uri' => 'Redirect URIs',

// The developer key, you get this at https://code.google.com/apis/console
'developer_key' => 'API key',

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

aa-04.structure-folder

edit file google-login.php

<?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
session_start();

$gClient = new Google_Client();

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

$google_oauthV2 = new Google_Oauth2Service($gClient);

$authUrl = $gClient->createAuthUrl();

//HTML page start
echo '<html xmlns="http://www.w3.org/1999/xhtml">';
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 '
<style>

.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;
}

</style>';

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

edit file google-view-profile.php

<?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
session_start();

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

$google_oauthV2 = new Google_Oauth2Service($gClient);

// get the code
if (isset($_GET['code'])) {
$gClient->authenticate($_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 '
<pre>';
print_r($user);
echo '</pre>
';
}

lakukan pengetesan

buka url

aa-05.google-button-login

halaman permintaan permissionaa-06.google-request-permission

halaman data-data user yang login

aa-07.login-success

Published by

G3n1k

just to remember what i had known :)

One thought on “PHP Login with Google Account”

Comments are closed.