How to login with gmail on website? Reviewed by Momizat on . First creat a New Project on google go to:- https://code.google.com/apis/console/   Fill all detail of project Then Get API PHP code google-login.php on we First creat a New Project on google go to:- https://code.google.com/apis/console/   Fill all detail of project Then Get API PHP code google-login.php on we Rating: 0

How to login with gmail on website?

First creat a New Project on google go to:-

https://code.google.com/apis/console/

Screen Shot 2014-05-01 at 15.23.14

 

Fill all detail of project Then Get API

gmail

PHP code google-login.php on website

require_once 'google/Google_Client.php';
require_once 'google/contrib/Google_Oauth2Service.php';
$res=array();
$scriptUri ="http://www.site.com/google-login.php";
$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('ABClogin');
$client->setClientId('xxxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxxxxxx');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$oauth2 = new Google_Oauth2Service($client);

$logout=(isset($_GET['logout'])?$_GET['logout']:'');
if (!empty($logout)) { 
	$this->_ci->session->set_userdata("token",'');
	die('Logged out.');
}

$code=(isset($_GET['code'])?$_GET['code']:'');
if (!empty($code)) { 
	$client->authenticate();
	$_SESSION['token'] = $client->getAccessToken();
	
	
	$redirect = $scriptUri;
	header('Location: '.filter_var($redirect, FILTER_SANITIZE_URL));
	return;
}
$session_id = $_SESSION['token'];
if (!empty($session_id)) { 
	$token = $session_id;
	$client->setAccessToken($token);
}

if (!$client->getAccessToken()) { 
	$authUrl = $client->createAuthUrl();
	header("Location: ".$authUrl);
	die;
}
if ($client->getAccessToken()) {
	$user = $oauth2->userinfo->get();
	
	if(!empty($user)){
	if(array_key_exists('email',$user)) $res['email'] = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
	if(array_key_exists('picture',$user)) $res['img'] = filter_var($user['picture'], FILTER_VALIDATE_URL);
	if(array_key_exists('given_name',$user)) $res['users_fname']=$user['given_name'];
	if(array_key_exists('family_name',$user)) $res['users_lname']=$user['family_name'];
	if(array_key_exists('gender',$user)) $res['gender']=$user['gender'];
	$res['id']=$user['id'];
	}
	print_r($res);
	
	
}

Leave a Comment

© 2014 Powered By