Přihlášování přes Google

23.09.2017 Programování #google #php #programování

Ukázka přihlašování prostřednictvím Google API a získání informací uživatele.


V první řadě se musí vytvořit projekt v Google Developers Console. V horní části je roletová nabídka, kde lze vybrat existující projekt nebo lze vytvořit nový. Při novém projektu se musí zapnout Google+ API. Nastavit Credential na OAuth client ID a typ aplikace Web Application.

<?php
session_start();

//Include Google client library 
include_once 'inc/src/Google_Client.php';
include_once 'inc/src/contrib/Google_Oauth2Service.php';

/*
 * Configuration and setup Google API
 */
$clientId = 'Client ID';
$clientSecret = 'Secet ID';
$redirectURL = 'http://URLSITE/index.php';

//Call Google API
$gClient = new Google_Client();
$gClient->setApplicationName('Login to Google');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectURL);

$google_oauthV2 = new Google_Oauth2Service($gClient);

if(isset($_GET['code'])){
    $gClient->authenticate($_GET['code']);
    $_SESSION['token'] = $gClient->getAccessToken();
    header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));
}

if($_GET['action']=='logout'){
	$gClient->revokeToken();
}

if (isset($_SESSION['token'])) {
    $gClient->setAccessToken($_SESSION['token']);
}
?>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Login with Facebook using PHP</title>
		<style type="text/css">
		h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
		</style>
		
		<!-- Latest compiled and minified CSS -->
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		
		<!-- Optional theme -->
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
		
		<!-- Latest compiled and minified JavaScript -->
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
		
	</head>
	<body>
	<div class="container" style="margin-top: 30px;">

	<?php
	if ($gClient->getAccessToken()) {
		//Get user profile data from google
		$gpUserProfile = $google_oauthV2->userinfo->get();
		
		//Insert or update user data to the database
		$gpUserData = array(
			'oauth_provider'=> 'google',
			'oauth_uid'     => $gpUserProfile['id'],
			'first_name'    => $gpUserProfile['given_name'],
			'last_name'     => $gpUserProfile['family_name'],
			'email'         => $gpUserProfile['email'],
			'gender'        => $gpUserProfile['gender'],
			'locale'        => $gpUserProfile['locale'],
			'picture'       => $gpUserProfile['picture'],
			'link'          => $gpUserProfile['link']
		);
		
	} else {
		$authUrl = $gClient->createAuthUrl();
		$output = '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"> Sign in with Google</a>';
	}

	if(!empty($gpUserData)){
	?>

		<h2>Google Profile Details | VOXCAFE</h2>
		<table class="table table-hover">
			<tr>
				<th>Profile Image</th>
				<th>Google ID</th>
				<th>Fullname</th>
				<th>Email</th>
				<th>Gender</th>
				<th>locale</th>
				<th>Visit Facebook</th>
				<th>Logout</th>
			</tr>
			<tr>
				<td><img src="<?php echo $gpUserData['picture']; ?>" class="img img-circle" style="width:100px;"></td>
				<td><?php echo $gpUserData['oauth_uid']; ?></td>
				<td><?php echo $gpUserData['first_name'].' '.$gpUserData['last_name']; ?></td>
				<td><?php echo $gpUserData['email']; ?></td>
				<td><?php echo $gpUserData['gender']; ?></td>
				<td><?php echo $gpUserData['locale']; ?></td>
				<td><a href="<?php echo $gpUserData['link']; ?>">Visit Google Link</a></td>
				<td><a href="index.php?action=logout">Logout</a></td>
			</tr>
		</table>

	<?php
	}else{
		echo $output;
	}
	?>

</div>
</body>
</html>

V souboru index.php stačí nastavit konstanty: clientID a clientSecret, které jsou dostupné v Google Developer Console při zakládání projektu.

DEMO DOWNLOAD