Created
September 3, 2013 16:23
-
-
Save miguelbalparda/6426127 to your computer and use it in GitHub Desktop.
Google+ Auth for Google apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'google-api-php-client/src/Google_Client.php'; | |
require_once 'google-api-php-client/src/contrib/Google_PlusService.php'; | |
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php'; | |
// Set your cached access token. Remember to replace $_SESSION with a | |
// real database or memcached. | |
session_start(); | |
$client = new Google_Client(); | |
$client->setApplicationName('Google+ PHP Starter Application'); | |
// Visit https://code.google.com/apis/console?api=plus to generate your | |
// client id, client secret, and to register your redirect uri. | |
$client->setClientId(''); | |
$client->setClientSecret(''); | |
$client->setRedirectUri(''); | |
$client->setDeveloperKey(''); | |
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', | |
'https://www.googleapis.com/auth/plus.me')); | |
$plus = new Google_PlusService($client); | |
if (isset($_GET['code'])) { | |
$oauth2 = new Google_Oauth2Service($client); | |
// Authenticate the user, $_GET['code'] is used internally: | |
$client->authenticate(); | |
// Will get id (number), email (string) and verified_email (boolean): | |
$user = $oauth2->userinfo->get(); | |
if ($user["hd"] == "santexgroup.com") { | |
//set user if the domain is correct | |
$_SESSION['token'] = $client->getAccessToken(); | |
} | |
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | |
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); | |
} | |
if (isset($_SESSION['token'])) { | |
$client->setAccessToken($_SESSION['token']); | |
} | |
if (!$client->getAccessToken()) { | |
$authUrl = $client->createAuthUrl(); | |
print '<a href="'.$authUrl.'""><img src="img/login.png"></a>'; | |
} else { //we are logged in! | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment